Can't get JsonObject value using Aquery in Android

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

, why?

Is there something wrong with this code or method?

//to get the 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 java class activity:

fetchCodeData(getApplicationContext());

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

      

The result looks like this: after calling the method, it returns null, but called the method again and got the json value? I do not know? Why is 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 supports multi-page writing such as uploading an image to a service. Just do a regular POST and put a byte array, Inputstream Could you name my mistake where I am going wrong.

+3


source to share


2 answers


Using Aquery is an easy way to get data from the server. If you are using a header and a parameter, do this:

Make sure the following permission is included in the manifest.

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

      



and the code you have to implement is

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, maybe the job will be prefect. For more on Aquery, go to the full documentation: https://code.google.com/p/android-query/wiki/AsyncAPI

+1


source


@BB: your final code should be,

public static void fetchCodeData(Context context2) {
    AQuery aQuery = new AQuery(context2);
    if(CheckInternetConnection.isConnectingToInternet()) {
        aQuery.ajax(url, AllOverApplicationVariable.accessCode, getGrandCodeCallback());
    }
}

      



Maybe it will work.

0


source







All Articles