Skip to content Skip to sidebar Skip to footer

Android Volley - Can't Do A Json Request With Post, Getting Empty Response

I am trying to make a json request using Volley library to a php server, but for some reason the server does not receive the json object I am sending, and it responds with empty st

Solution 1:

Make sure it's not a server side error (try your service using Postman for instance).

I personally faced the same issue some time ago, changing JsonObjectRequest to StringRequest fixed my problem.

Take a look at this link : https://stackoverflow.com/a/31613565/7871886

Now I use Retrofit2 instead of Volley... Could be an option. Happy coding

Solution 2:

Not sure what was your problem, but for the future googlers:

My problem was that I was trying to read form $_POST instead of php://input

Full code (working):

Java:

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose
jsonobj = newJSONObject();
try {
    // adding some keys
    jsonobj.put("new key", Math.random());
    jsonobj.put("weburl", "hashincludetechnology.com");
    // lets add some headers (nested JSON object)JSONObject header = newJSONObject();
    header.put("devicemodel", android.os.Build.MODEL); // Device model
    header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version
    header.put("language", Locale.getDefault().getISO3Language()); // Language
    jsonobj.put("header", header);
    // Display the contents of the JSON objects
    display.setText(jsonobj.toString(2));
} catch (JSONException ex) {
    display.setText("Error Occurred while building JSON");
    ex.printStackTrace();
}

JsonObjectRequest jsObjRequest = newJsonObjectRequest(Request.Method.POST, URL, jsonobj, newResponse.Listener<JSONObject>() {


    @OverridepublicvoidonResponse(JSONObject response) {
        System.out.println("onResponse()");

        try {
            result.setText("Response: " + response.toString(2))

            System.out.println("Response: " + response.toString(2));
        } catch (JSONException e) {
            display.setText("Error Occurred while building JSON");
            e.printStackTrace();
        }
        //to make sure it works backwards as well

    }
}, newResponse.ErrorListener() {

    @OverridepublicvoidonErrorResponse(VolleyError error) {
        System.out.println("onErrorResponse()");
        System.out.println(error.toString());


    }
});


System.out.println("After the request is made");
// Add the request to the RequestQueue.
queue.add(jsObjRequest);

Clarification: display and result are two TextView objects I am using to display data on the screen, and queue is Volley's request queue.

PHP:

$inp = json_decode(file_get_contents('php://input')); //$input now contains the jsonobjecho json_encode(["foo"=>"bar","input"=>$inp]); //to make sure we received the json and to test the response handling

Your Android Monitor should output sth. like :

{"foo":"bar","input":{"new key":0.8523024722406781,"weburl":"hashincludetechnology.com","header":{"devicemodel":"Android SDK built for x86","deviceVersion":"7.1","language":"eng"}}}

Post a Comment for "Android Volley - Can't Do A Json Request With Post, Getting Empty Response"