androidjsonaquery

Cannot get JsonObject value using Aquery in Android


Using this approach https://code.google.com/p/android-query/wiki/AsyncAPI ,I made this code for getting json data from api, but getting null, why?

Is something wrong with this code or method is wrong?

// get code

public static void fetchCodeData(Context context2) {
    AQuery aQuery = new AQuery(context2);
    if(CheckInternetConnection.isConnectingToInternet()) {
        aQuery.ajax(url, null, JSONObject.class, getGrandCodeCallback());

    }
}

and

private static AjaxCallback<JSONObject> getGrandCodeCallback() {
    return new AjaxCallback<JSONObject>() {

        private String accessCode;

        @Override
        public void callback(String url, JSONObject object, AjaxStatus status) {
            // get data here
Log.e("Object", "object=" + object.toString());
            if (object != null) {
                try {
                    accessCode = object.getJSONObject("data").getString("code");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }.header("Content-Type", "application/json; charset=utf-8")
    .header(Client_ID, Client_ID)
    .header(Client_SECRET, Client_SECRET);
}

Calling on java class activity :

fetchCodeData(getApplicationContext());

Log.e("getCallback",
            "AllOverApplicationVariable.accessCode==="
                + AllOverApplicationVariable.accessCode);

Output looks like this, after method call it return null but again called method and get json value ? i dont know ? why this ?

06-16 17:58:29.196: E/getCallback(12435): AllOverApplicationVariable.accessCode===null

06-16 17:58:30.202: E/Object(12435): object={"data":{"state":"","scope":"","expires":"2015-06-17 17:58:30","code":"69e11cfb3243244da5dc6b0aac1515569c06d2d2","response_type":"code","client_id":"1","redirect_uri":"http://localhost/eads"}}

AQuery support multipart post such as uploading an image to a service. Simply do a regular POST and put byte array, Inputstream Could you please identify my error, where i am doing wrong.


Solution

  • Using Aquery is simple to get data from server. If you are using header and parameter then do this:

    Make sure the following permission is included in your manifest.

      <uses-permission android:name="android.permission.INTERNET" /> 
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    and Code you have to implement,

    AQuery aQuery = new AQuery(mcontext);
        Map<String, Object> params = null;
        JSONObject mainJson = new JSONObject();
        try {
        // this is paramters
            mainJson.put("username", "jack");
            mainJson.put("pwd", "jack");
            StringEntity sEntity = new StringEntity(mainJson.toString(), HTTP.UTF_8);
            params = new HashMap<String, Object>();
            params.put(AQuery.POST_ENTITY, sEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        if (CheckInternetConnection.isConnectingToInternet()) {
            aQuery.ajax(Url.urlUserHistoryAdList, params, JSONObject.class, new AjaxCallback<JSONObject>() {
            @Override
            public void callback(String url, JSONObject json, AjaxStatus status) {
                if (json != null) {
                String jsonString=json.toString();
                // Do whatever want in your fetched json from server
                } else {
                // json not getting properly, json data null
                }
    
            }
            }.header("Content-Type", "application/json; charset=utf-8").header(token, "123456789"));
    

    So please try this, may be work prefectly. More details about Aquery , go to full documentation:https://code.google.com/p/android-query/wiki/AsyncAPI