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?