0

I need to make GET-request to a url consisting JSON-data and try to use Loopj library, but it returnes nothing. I tried to find examples on the Internet but they turned out, maybe, to be obsolete (onSuccess method had different parameters). I tried to adapt my code to that example and what I got:

String AllData=""; AsyncHttpClient client = new AsyncHttpClient();
    client.get("wantedUrl", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            for (byte aResponseBody : responseBody) {
                AllData += aResponseBody;
            }

        }
        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Toast.makeText(getApplicationContext(),"We got an error",Toast.LENGTH_SHORT).show();
        }
    });

"wantedUrl" is the url with json, but "AllData" remains empty. How to fix it?

Justin McGuire
  • 365
  • 1
  • 5
  • 18

1 Answers1

0

You're getting AsyncHttpResponseHandler callback where you're in need of JsonHttpResponseHandler. Use below code to get the things right on track.

 String AllData=""; AsyncHttpClient client = new AsyncHttpClient();
        client.get("wantedUrl", new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                super.onSuccess(statusCode, headers, response);

                //Here response will be received in form of JSONArray
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);

                //Here response will be received in form of JSONObject
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(getApplicationContext(), "We got an error", Toast.LENGTH_SHORT).show();
            }
        });
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
  • It works, but how to record the response into a variable of Activity class? (I declared it as a "public static" to access it in onSuccess method, but then outside the above construction it has its previous value (but in onSuccess everything is ok - I tested it with the same Toast) – Justin McGuire Jul 24 '15 at 07:36
  • @JustinMcGuire you need to learn basic concepts of JAVA for this. No Offence. – Nitin Misra Jul 24 '15 at 07:45
  • I know that I'm kinda newbie, but anyway I need to record response data into arrays to inflate ListView - so what is the way I can do it? – Justin McGuire Jul 24 '15 at 15:57
  • @JustinMcGuire you just need not to do that, you've all JSON here in `response` write your iteration code here and put data into arraylist here in `onSuccess()`, you're good to go, try to ready some good articles, you'll easily find plenty of them. – Nitin Misra Jul 24 '15 at 17:00
  • And I think the last question - how can I get JSONObject from that JSONArray I received from URL using the second onSuccess method from above, not just "getJSONObject – Justin McGuire Jul 25 '15 at 07:48