Android Volley Post Request,发送JSONObject,获取字符串响应

时间:2016-07-27 18:29:12

标签: java android android-volley

我目前正在使用JSONObjectRequest从服务器获取数据。我发送JSON数据,但我希望得到一个字符串响应。 Volley返回一个错误(当然是一个空错误,所以我不知道发生了什么)。这是因为我使用了错误的请求吗?我的代码是:

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST,
            postURL.toString(),
            getJson(),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); Log.v(TAG, "Error: " + error.getCause()); }
            }
    );

我的logcat包含以下数据:

07-27 11:45:57.218 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 4
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-,err=8
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 1024
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-, 1
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy+
07-27 11:45:57.228 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy-, success
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 4
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-,err=8
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo+,hn 21(0x6670742e6d6963),sn(),family 0,flags 1024
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo-, 1
07-27 11:46:02.258 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy+
07-27 11:46:02.288 11309-12374/com.microsoft.quiztest D/libc: [NET] getaddrinfo_proxy-, success
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest V/DeviceForensics: Error: null
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err: com.android.volley.TimeoutError
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:141)
07-27 11:46:12.368 11309-11309/com.microsoft.quiztest W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

1 个答案:

答案 0 :(得分:0)

首先,您需要确保您的服务器为您提供JsonObjectRequest而不是JsonArrayRequest

如果不是这种情况,可以确认您不需要标题,或者如果您使用Request.setRetryPolicy()更改超时持续时间。

希望这有帮助,

    mRequestQueue = Volley.newRequestQueue(context);
    final String url = "url";
    final ProgressDialog pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Loading data...");
    pDialog.show();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, params,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAGOnResponse", response.toString());
                    pDialog.hide();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            String json = null;

            NetworkResponse response = error.networkResponse;
            if(response != null && response.data != null){
                switch(response.statusCode){
                    case 400:
                        json = new String(response.data);
                        json = trimMessage(json, "message");
                        if(json != null) displayMessage(json);
                        Log.i("ResponseJSON", json);
                        break;
                    case 401:
                        json = new String(response.data);
                        json = trimMessage(json.toString(), "message");
                        if(json != null) displayMessage(json);
                        break;
                    case 500:
                        json = new String(response.data);
                        json = trimMessage(json, "message");
                        if(json != null) displayMessage(json);
                        break;
                }
            }
            VolleyLog.d("TAGOnError " + error.getMessage(), "Error: " + error.getMessage());
            pDialog.hide();
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("X-Usuario-Email", email);
            headers.put("X-Usuario-Token", token);
            return headers;
        }
    };
    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    mRequestQueue.add(jsonObjReq);
    mRequestQueue.addRequestFinishedListener(this);
}

@Override
public void onRequestFinished(Request<Object> request) {
    if (request.getUrl().contains("Something")) {
        // Success asynchronous call
    }
}