【问题标题】:Sugar get_entry_list returns "Invalid Session ID" when querying Meetings for a specific userSugar get_entry_list 在查询特定用户的会议时返回“无效的会话 ID”
【发布时间】:2012-01-28 11:06:41
【问题描述】:

我尝试使用 Java 通过其 REST API 查询 Sugar,以获取特定用户(即当前登录的用户)在会议模块中的条目。

我已经尝试了几天,同时在谷歌上搜索解决方案。

我调用了 login(),得到了一个会话 ID,然后调用了 get_user_id()。使用返回的用户 ID,我尝试使用 get_entry_list() 查询会议模块。

要获得分配给用户 ID 的会议,它使用以下查询字符串,其中 mUserId 保存 get_user_id() 返回的用户 ID:

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

但我不仅想获得分配给用户的会议,还想获得他参加的所有会议。为此,我在查询中尝试对 meeting_users 表进行子查询。

这是我尝试过的查询字符串,它在 MySQL 提示符下工作。但是当我通过 REST 尝试这个时,它返回“无效的会话 ID”:

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

有人对此有提示吗?哪些情况会导致“无效会话 ID”?

什么也不起作用,例如正在将“and deleted = '0'”附加到第一个声明的查询中:

queryString = "meetings.assigned_user_id='"+mUserId+"' and 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();

login() 返回有效的会话 id,getUserId() 返回正确的 id。整个代码已经用于获取联系人,也可以用于上述的简单查询。

提前致谢

马克

【问题讨论】:

    标签: java android soap sugarcrm


    【解决方案1】:

    经过进一步测试,我意识到查询字符串中的空格是问题所在。它们导致一个包含空格的 URL。为避免必须进行某种 URL 编码。

    我没有成功在我的 httpPost 方法中对整个 URL 进行编码(似乎没有必要)。但是在查询字符串中用“+”替换空格对我有用:

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

    如果有人有更优雅的方法,请告诉我。

    【讨论】:

      【解决方案2】:

      使用 get_relationships 网络服务调用可能会更好。

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

      这应该就是你所需要的。在“会议”之后的参数中,您还可以传入一个额外的过滤器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        • 2012-03-22
        相关资源
        最近更新 更多