【问题标题】:Get JSON response containing Github user list using AsyncHttpClient使用 AsyncHttpClient 获取包含 Github 用户列表的 JSON 响应
【发布时间】:2014-11-06 19:34:35
【问题描述】:

我正在尝试从 github api 获取用户 json 列表的响应。

谁能告诉我代码执行路径没有到达重写方法 onFailure()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;
}

出于某种原因,这些方法只是被忽略了。在 client.get(...) 之后,它会直接跳转到 return content

关于它的原因有什么想法吗?我做错了什么?

不胜感激。


编辑: 所以正确的方法是在 onSuccess(...) 方法中进行响应操作?

                    @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();
                    }
                }

我尝试从响应中解析信息,例如:

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();
    }
}

但该代码仍然不起作用。在事件驱动开发中,它由鼠标移动、按键等引起。是什么导致这些 onSuccess()onFailure() 发生?前提是什么?

【问题讨论】:

    标签: android json github android-async-http asynchttpclient


    【解决方案1】:

    您正在使用 AsyncHttpClient。你知道“异步”是什么意思吗?

    是“异步”的缩写,意思是“独立于主程序流程发生”。您的主程序线程在继续之前不会等待 AsyncHttpClient 完成其请求。这就是您的 getResponse() 方法立即返回的原因。

    您应该按照事件驱动编程原则设计您的应用程序;这意味着,在您的情况下,您将生成处理来自onSuccess() 接口方法的响应所需的任何进程或方法。

    因此,您可能不会从getResponse() 返回任何内容。您可以将其设为void。您正在实例化并传递给get() 方法的AsyncHttpResponseHandler() 是一个接口,当某些事件发生时(即GET 请求成功或失败时,库将调用该接口。 p>

    最后,您必须以某种方式显示您的数据。我假设它在 ListView 中?如果是这样,您将需要通知 ListView 其数据已更改(或可能使用新数据重新创建适配器)。见this question。另外,我不确定,但您可能必须使用 runOnUIThread() 来更新您的视图,因为这是在不同的线程上运行的。

    【讨论】:

    • 感谢您的回答!当您在不阅读其文档的情况下使用新库时会发生这种情况。据我了解,使用它的正确方法是在 onSuccess() 方法中处理响应。你能检查一下编辑过的帖子吗?
    • 是的,getResponse() 现在不返回值。是的,为了显示数据,我使用适配器和mAdapter.notifyDataSetChanged()。 ListView 的内容在拉取时更新(拉取刷新列表)。但有趣的是,在第一次拉动时没有任何反应,因此不会调用 onFailure()onSuccess。但是每次下一次拉动我都会从onFailure()方法得到一个错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多