Android Volley onResponse获取数据

时间:2016-11-08 20:17:57

标签: android android-volley jsonresponse

在登录的字符串post请求中。在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();
    }
}

2 个答案:

答案 0 :(得分: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而不实施getBodyparseNetworkResponsegetBodyContentType可能会更好。

答案 1 :(得分:1)

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

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