Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:09:08+00:00 2026-05-29T10:09:08+00:00

I try to query Sugar through its REST API using Java for entries in

  • 0

I try to query Sugar through its REST API using Java for entries in Meetings module for a specific user, namely the one who is logged in currently.

I am trying this a few days already while googling around for asolution.

I made a login() call, where I got a session ID, than I make a call to get_user_id(). With the returned user ID I try to query the Meetings module by using get_entry_list().

To get the Meetings assigned to the UserID it works with following query string, where mUserId holds the returned user id of get_user_id():

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

But I not only want to get the meetings, where a user is assigned to, but all Meetings where he participates. For that I try a subquery on meetings_users table in my query.

Here is a query strings I tried, which os working on MySQL prompt. But when I try this over REST, it returns “Invalid Session ID”:

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

Does anyone have a hint on this? Which conditions lead to an “Invalid Session ID” at all?

What also does not work e.g. is appending “and deleted = ‘0’” to the first stated query:

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

also fails.

As requested here is the full code example, platform is Android, API Level 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();
}

Calling the code above:

        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() returns valid session id, and getUserId() returns right id. The whole code is already working for fetching contacts, and also working for a simple query as stated above.

Thanks in advance

Marc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:09:09+00:00Added an answer on May 29, 2026 at 10:09 am

    After further testing, I realized, that whitespaces in the query string are the problem. They lead to an URL containing whitespace. To avoid that some kind of URL encoding has to be done.

    I had not success in encoding the whole URL in my httpPost methods (seems not to be necessary). But replacing spaces with ‘+’ in the query string works for me:

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

    If anyone has a more elegant method of doing this, please let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following HQL query to try and load a set of objects
When I try to query a table using CreateSqlQuery and convert it to an
I am trying to display the data using parametrized query try { SqlConnection xconn
Whenever I try a query like: mysql_query(SELECT * FROM data WHERE `user`=$_SESSION['valid_user'] LIMIT 1);
I'm using SQL Server 2008 with Report Builder 2.0 to try and query data
When I try to query foreign keys using get(), I always get None values,
I'm trying to use Subsonic 3.0 but with every query I try it gives
If I try to run this query in SQL Server 2005: SELECT 1 WHERE
When I try to run any update query in Access 2007, I get the
I could try to post and explain the exact query I'm trying to run,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.