3

我对 Android Studio 很陌生,我正在尝试使用 Volley 构建一个获取请求,服务器响应是一个 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;
    }
4

1 回答 1

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();
    }
}
于 2017-04-23T09:39:47.073 回答