0

I'm currently using a JSONObjectRequest to get data from a server. I'm sending JSON data, but I expect a string response. Volley returns an error (a null error of course, so I have no idea what's going on). Is this because I'm using the wrong request? My code is:

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

My logcat contains the following data:

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)
Jacob Denson
  • 391
  • 1
  • 4
  • 14
  • Post your logcat, as well as any error log in server. – Bonatti Jul 27 '16 at 18:47
  • Can you [check this answer](http://stackoverflow.com/a/33600251/4903925) and try some different timeouts. Where is your Host? Local? Internet? Internal? Can you access it with another resource/machine? Can you ensure that the mime types agreed upon are correct? – Bonatti Jul 27 '16 at 19:01
  • you are not getting any response .simply your server or your request is taking to much time to for execution so volley gives you timeout error .it happening maybe you are sending a huge data like image or something or due to network connectivity issue or your server code is not sending any response – Pavneet_Singh Jul 28 '16 at 06:40
  • First, you might want to check if the url that you are passing to volley is valid. If that is not the case, have you tried with a `StringRequest` instead of `JsonObjectRequest`? – ffs Jul 28 '16 at 14:14
  • It's a valid URL - I can connect to the URL on my pc and on my iPhone. However, I can't connect to the URL on my android browser? Has anyone encountered this before? – Jacob Denson Jul 28 '16 at 21:12
  • figured it out - it was a certificate error, so I was refused connection to the server. Now I have to load certificates in my app :( – Jacob Denson Jul 29 '16 at 05:23

1 Answers1

0

First of all you need to be sure that your server is giving you a JsonObjectRequest and not a JsonArrayRequest.

If is not the case maybe verify that you do not need a header, or maybe if you change the timeout duration with Request.setRetryPolicy().

Hope this helps,

    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
    }
}
Snoobie
  • 760
  • 2
  • 12
  • 30