0

I'm trying to get response with user json list from github api.

Could anyone tell me the reason why code execution path doesn't reach overriden methods onFailure() and onSuccess?

public String getResponse()
{
    AsyncHttpClient client=new AsyncHttpClient();
    RequestParams params=new RequestParams();
    params.put("since","0");
    client.get("https://api.github.com/users", params, new AsyncHttpResponseHandler() {

                @Override
                public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                      Throwable arg3) {
                    userList.clear();
                    userList.addFirst("Items didn't load properly");

                }

                @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

                    try {
                        content = new String(arg2, "UTF-8");
                        //content is json response that can be parsed.
                    } catch (UnsupportedEncodingException e1) {
                        userList.clear();
                        userList.add("Some encoding problems occured");
                        e1.printStackTrace();
                    }
                }
            });
    return content;
}

These methods just ignored for some reason. After client.get(...) it jumps right to return content.

Any ideas about the reason of it? What am I doing wrong?

Would appreciate any advice.


EDIT: SO the proper way is to do that is to operate with response within the onSuccess(...) method?

                    @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

                    try {
                        content = new String(arg2, "UTF-8");
                        //content is json response that can be parsed.

                        parseResponseAmount(content, 10); //operate with response 
                    } catch (UnsupportedEncodingException e1) {
                        userList.clear();
                        userList.add("Some encoding problems occured");
                        e1.printStackTrace();
                    }
                }

And I try to parse information from response like:

private  void parseResponseAmount (String response, int amount)
{
    try {
        JSONArray readerArray = new JSONArray(response);
        for (int i = 0; i < amount; i++)
        {
            JSONObject userObject = (JSONObject) readerArray.get(i);
            String login = userObject.getString("login");
            getUserList().add(login);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        getUserList().clear();
        getUserList().add("Failed to parse response");
        e.printStackTrace();
    }
}

but that code still doesn't work. In event-driven development it caused by mouse move, key presses etc. What causes these onSuccess() and onFailure() to occur? What is the precondition?

AnZyuZya
  • 201
  • 1
  • 3
  • 18

1 Answers1

1

You're using AsyncHttpClient. Do you know what "Async" means?

It is a shortening of "Asynchronous", which means "occurring independently of the main program flow". Your main program thread will not wait for the AsyncHttpClient to complete its request before continuing. That's why your getResponse() method return immediately.

You should design your app to operate on event-driven programming principles; this means, in your case, that you would spawn whatever process or method you need to handle the response from the onSuccess() interface method.

So, you probably won't be returning anything from getResponse(). You can make it void. The AsyncHttpResponseHandler() you're instantiating and passing to the get() method is an interface which the library will call when certain events take place—that is, when the GET request succeeds or fails.

Finally, you will have to show your data somehow. I'm assuming it is in a ListView? If so, you will need to notify the ListView that its data has changed (or possibly recreate the adapter with new data). See this question. Also, I'm not sure but you may have to use runOnUIThread() to update your views, since this is running on a different thread.

Community
  • 1
  • 1
JstnPwll
  • 8,585
  • 2
  • 33
  • 56
  • Thanks for answer! That what happens when you work with new libraries without reading its documentation.. As far as I understood you, the proper way to work with it is to handle response within **onSuccess()** method. Could you please check edited post? – AnZyuZya Nov 07 '14 at 12:36
  • yes, `getResponse()` doesn't return value now. Yes, to show data I use adapter and `mAdapter.notifyDataSetChanged()`. ListView's content is updated on pull (pull-to-refresh list). But the funny thing is that on first pull nothing happens so `onFailure()` or `onSuccess` isn't invoked. But every next pull I'm getting an error from `onFailure()` method – AnZyuZya Nov 07 '14 at 14:31