【问题标题】:Android Volley onResponse get dataAndroid Volley onResponse 获取数据
【发布时间】:2016-11-08 20:17:57
【问题描述】:

在用于登录的字符串发布请求中。在 onResponse 中,我返回的只是状态代码 200。我也想获取返回的数据。我不知道从现在开始往哪里走?关于如何获取返回数据的任何想法,而不仅仅是状态码?

    public void requestWithSomeHttpHeaders() {

    try {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String URL = "http://......";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("username", "yourusername");
        jsonBody.put("password", "yourpassword");
        final String mRequestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                    return null;
                }
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                    // can get more details such as response.headers
                    System.out.println(responseString);
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 除了onResponseonError 之外真的有必要实现吗?

标签: android android-volley jsonresponse


【解决方案1】:

最好定义一个方法。

private void doLogin() {
    // TODO: startActivity?
}

然后调用它

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);

            doLogin();
        }

如果你想要来自服务器的数据,那就是String response。检查你的 Logcat。

如 cmets 中所述,使用 StringRequest 而不实现 getBodyparseNetworkResponsegetBodyContentType 可能会更好。

【讨论】:

  • 在 logcat 中我看到的只是 I/VOLLEY: 200
  • 如果我在 Google Postman 中执行类似操作,我会返回各种数据。
  • 我不知道你的网址是什么,但它似乎只是返回"200"。如果这是一个问题,那就是服务器端的问题,而不是 Volley 问题
  • 尝试使用StringRequest而不实现getBodyparseNetworkResponsegetBodyContentType
  • 成功了,谢谢!我的 API 当前需要在正文中发送的用户名和密码。我删除了 parseNetworkResponse,并重新设计了 getBody 以返回 mRequestBody.toString().getBytes();
【解决方案2】:

如果你想像这样解析 JSON 添加方法

 public Profile getUserProfile(String response){
    Profile profile = null;
    try {
        profile = new Profile();
        JSONObject jsonObject = new JSONObject(response);
            profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
        profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
        profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
        profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));


    } catch (JSONException | NullPointerException e) {
        e.printStackTrace();
        return null;
    }

    return profile;
}

在你的 onResponse 方法中

     @Override
        public void onResponse(String response) {
            //If you have son object
           Profile profile = getUserProfile(response)
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多