【问题标题】:Volley Get Request: onResponse is never calledVolley Get Request:永远不会调用 onResponse
【发布时间】:2017-04-23 07:53:32
【问题描述】:

我对 Android Studio 很陌生,我正在尝试使用 Volley 构建一个 Get 请求,服务器响应是一个 JsonObject。我用断点测试了代码,但我想知道为什么我不跳到 onResponse 或者为什么它不起作用。

这是我的 Get 方法代码:

public Account GetJsonObject(String urlExtension, String name, Context context) {
    String baseURL = "myurl.com/api";
    baseURL += urlExtension + "/" + name; 
    // url will look like this: myurl.com/api/user/"username"

    final Account user = new Account("","");
    //Account(name, email)
    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(context);

    JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
            new Response.Listener<JSONObject>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject obj = response.getJSONObject("userObject");

                        String username = obj.getString("Username");
                        String email = obj.getString("Email");
                        user.setUsername(username);
                        user.setEmail(email);

                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });


    requestQueue.add(jsonObject);
    return user;
    }

【问题讨论】:

  • 您的错误回调为空。你确定你在那里尝试过断点吗?

标签: android android-studio android-volley jsonobjectrequest


【解决方案1】:

正如@GVillavani82 评论你的onErrorResponse() 方法体是空的。尝试像这样记录错误

 new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
       Log.e("ERROR", "Error occurred ", error);   
     }
 }

确保您在AndroidManifest.xml 文件中设置了以下权限并且api URL 正确。

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

JsonObjectRequest 类返回异步网络调用类。修改您的代码,如下所示。

// remove Account return type and use void

public void GetJsonObject(String urlExtension, String name, Context context) {
....
....  // other stuffs
....

JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
        new Response.Listener<JSONObject>() {

            // Takes the response from the JSON request
            @Override
            public void onResponse(JSONObject response) {

                processResponse(response);  // call to method

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("ERROR", "Error occurred ", error);   
            }
        });

      requestQueue.add(jsonObject);
}

现在创建另一个方法,如下所示

private void processResponse(JSONObject response) {
    try {
         final Account user = new Account("","");
         JSONObject obj = response.getJSONObject("userObject");

         String username = obj.getString("Username");
         String email = obj.getString("Email");
         user.setUsername(username);
         user.setEmail(email);

    } catch (JSONException e) {
         e.printStackTrace();
    }
}

【讨论】:

  • 啊,好的,谢谢你和@GVillavani82 的帮助:D
  • 如果我的回答有助于解决您的问题,请通过检查我的回答左侧的右标记将我的回答视为已接受。
  • 不幸的是,现在我有时间尝试一下,但它不起作用,它跳入 ErrorListener.. 我使用的是正确的 url
  • 对不起,如果这是一个愚蠢的问题,我在哪里可以看到错误?我正在运行调试模式,没有发现任何错误。
  • 好的,谢谢,我现在自己试试,谢谢:)
猜你喜欢
  • 2018-05-30
  • 2020-11-18
  • 2021-07-27
  • 2021-04-15
  • 2014-11-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多