0

在用于登录的字符串发布请求中。在 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();
    }
}
4

2 回答 2

1

如果你想像这样解析 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)
        }
于 2016-11-08T20:28:14.233 回答
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。

如评论中所述,使用StringRequest不实施getBody,parseNetworkResponsegetBodyContentType可能会更好。

于 2016-11-08T20:22:10.780 回答