2012-01-28 6 views
2

特定のユーザー、つまり現在ログインしているユーザーの会議モジュールのエントリに対してJavaを使用してREST APIを通じてSugarにクエリを試みます。Sugar get_entry_listは、特定のユーザーの会議を照会するときに「無効なセッションID」を返します。

私はすでに解決を求めてグーグルでこれを数日間試しています。

私はget_user_id()を呼び出すよりもセッションIDを取得したlogin()コールを作成しました。返されたユーザーIDで、私はget_entry_list()を使ってMeetingsモジュールを照会しようとします。

それはmUserIdがGET_USER_IDの返されたユーザID()を保持している次のクエリ文字列、と連携ユーザーIDに割り当てられてミーティングを取得するには、次の

queryString = "meetings.assigned_user_id='"+mUserId+"'"; 

しかし、私はだけでなく、会議を取得したいがユーザーは割り当てられていますが、参加しているすべての会議に割り当てられます。そのために、私はクエリのmeetings_usersテーブルのサブクエリを試しています。

ここで私が試したクエリ文字列は、MySQLのプロンプトで動作します。しかし、私はRESTの上にこれをしようとすると、それは「無効なセッションID」を返します。

queryString = "meetings.id IN (SELECT meetings_users.meeting_id FROM meetings_users WHERE meetings_users.user_id = '"+mUserId+"')"; 

誰もがこの上のヒントを持っていますか?どのような条件で「無効なセッションID」になるのでしょうか?

また、どのように動作しませんか。

queryString = "meetings.assigned_user_id='"+mUserId+"' and deleted = '0'"; 

も、「およびdeleted = '0'」を追加しています。

ここで要求されたように、完全なコード例で、プラットフォームは、Android、APIレベル8である:上記のコードを呼び出す

private JSONArray getEntryList(String moduleName, 
     String selectFields[], String queryString, String orderBy, int max_results) throws JSONException, IOException, KeyManagementException, NoSuchAlgorithmException 
{ 
    JSONArray jsoSub = new JSONArray(); 
    if (selectFields.length > 0) 
    { 
     for (int i = 0; i < selectFields.length; i++) 
     { 
      jsoSub.put(selectFields[i]); 
     } 
    } 

      // get_entry_list expects parameters to be ordered, JSONObject does 
      // not provide this, so I built my JSON String on my own 
    String sessionIDPrefix = "{\"session\":\""+ mSessionId+ "\"," + 
      "\"modulename\":\""+ moduleName+ "\"," + 
      "\"query\":\""+ queryString + "\"," + 
      "\"order_by\":\""+ orderBy + "\"," + 
      "\"offset\":\""+ mNextOffset+ "\"," + 
      "\"select_fields\":["+ jsoSub.toString().substring(
        1, jsoSub.toString().length()-2)+ "\"],"+ 
      "\"max_results\":\""+ 20 + "\"}"; 

    String restData = sessionIDPrefix; 
    Log.d(TAG, restData); 

    String data = null; 
    String baseurl = mUrl + REST_URI_APPEND; 

    data = httpPost(baseurl+"?method=get_entry_list&input_type=json&response_type=json&rest_data="+restData); 

    Log.d(TAG, data); 
    JSONObject jsondata = new JSONObject(data); 

    mResultCount = jsondata.getInt("result_count"); 
    mNextOffset = jsondata.getInt("next_offset"); 

    return jsondata.getJSONArray("entry_list"); 
} 

private String httpPost(String urlStr) throws IOException{ 
    String urlSplitted [] = urlStr.split("/", 4); 
    String hostPort[] = urlSplitted[2].split(":"); 
    String hostname = hostPort[0]; 
    int port = 80; 
    if (hostPort.length > 1) 
     port = new Integer(hostPort[1]); 

    String file = "/"+urlSplitted[3]; 

    Log.d(TAG, hostname + ", " + port + ", " +file); 

    URL url = null; 
    try { 
     url = new URL("http", hostname, port, file); 
    } catch (MalformedURLException e) { 
     throw new IOException(mContext.getText(R.string.error_malformed_url).toString()); 
    } 

    Log.d(TAG, "URL "+url.toString()); 
    HttpURLConnection conn = null; 
    try { 
     conn = (HttpURLConnection) url.openConnection(); 
    } catch (IOException e) { 
     throw new IOException(mContext.getText(R.string.error_conn_creat).toString()); 
    } 
    conn.setConnectTimeout(60 * 1000); 
    conn.setReadTimeout(60 * 1000); 
    try { 
     conn.setRequestMethod("POST"); 
    } catch (ProtocolException e) { 
     throw new IOException(mContext.getText(R.string.error_post).toString()); 
    } 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.setUseCaches(false); 
    conn.setAllowUserInteraction(false); 
    conn.setRequestProperty("Content-Type", 
    "application/x-www-form-urlencoded"); 

    try { 
     conn.connect(); 
    } catch (IOException e) { 
     throw new IOException(mContext.getText(R.string.error_conn_open).toString() 
       + "\n" + e.getMessage()); 
    } 

    int response = 0; 
    String responseMessage = null; 
    try { 
     response = conn.getResponseCode(); 
     responseMessage = conn.getResponseMessage(); 
    } catch (IOException e) { 
     conn.disconnect(); 
     throw new IOException(mContext.getText(R.string.error_resp_io).toString()); 
    } 
    Log.d(TAG, "Exception Response "+ response); 
    if (response != 200) { 
     conn.disconnect(); 
     throw new IOException(mContext.getText(R.string.error_http).toString() 
       + "\n" + response + " " + responseMessage); 
    } 

    StringBuilder sb = null; 
    try { 
     BufferedReader rd = new BufferedReader(
     new InputStreamReader(conn.getInputStream())); 
     sb = new StringBuilder(); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      Log.d(TAG,"line " + line); 
      sb.append(line); 
     } 
     rd.close(); 
    } catch (IOException e) { 
     conn.disconnect(); 
     throw new IOException(mContext.getText(R.string.error_resp_read).toString()); 
    } 

    conn.disconnect(); 

    if (sb.toString() == null) 
    { 
     throw new IOException(mContext.getText(R.string.error_resp_empty).toString()); 
    } 

    return sb.toString(); 
} 

 if (login() != OK) 
    return null; 

    mResultCount = -1; 
    mNextOffset = 0; 

    mUserId = getUserId(); 

    String fields[] = new String [] { 
     "id", 
     "name", 
     "description", 
     "location", 
     "date_start", 
     "date_end", 
     "status", 
     "type", 
     "reminder_time", 
     "parent_type", 
     "parent_id", 
     "deleted", 
     "date_modified" 
    }; 

    String queryString = null; 
    if (syncAllUsers) 
     queryString = ""; 
    else 
    { 
     queryString = "meetings.assigned_user_id = 'seed_sarah_id' and meetings.deleted = '0'"; 
     //queryString = "meetings.id IN (SELECT meeting_id FROM meetings_users WHERE user_id ='"+mUserId+"'"; 
    } 

    entryList.clear(); 

    while (mResultCount != 0) 
    { 
     if (!seamless_login()) 
      return null; 

     JSONArray serverEntryList = getEntryList( 
       "Meetings", fields, queryString, "date_start", 0); 

     //... do sth with data 
     } 
     totalContactsResults += mResultCount; 
    } 
    logout(); 

ログインを()有効なセッションIDを返し、getUserId ()は正しいIDを返します。上記のように、コード全体がすでに連絡先を取得しているだけでなく、単純なクエリで作業しています。事前に

おかげ

マルク・

答えて

1

さらにテストした結果、クエリ文字列の空白が問題であることがわかりました。空白を含むURLにつながります。何らかの種類のURLエンコーディングが行われないようにする。

httpPostメソッドでURL全体をエンコードするのに成功しませんでした(必要ではないようです)。しかし、クエリ文字列に「+」とスペースを交換すると私の作品:誰もがこれを行うためのよりエレガントな方法があります

queryString = "meetings.id+IN+(SELECT+meetings_users.meeting_id+FROM meetings_users+WHERE+meetings_users.user_id='"+mUserId+"')"; 

場合、私に知らせてください。

1

おそらくget_relationshipsのWebサービス呼び出しを使用したほうが良いでしょう。

SugarRest.call('get_relationships', [SugarRest.session, 'Users', SugarRest.user_id, 'meetings', '', ['name'], 0, '']) 

これですべてが必要です。 'ミーティング'後のパラメータでは、追加のフィルタを渡すこともできます。

関連する問題