sigillum_conf sigillum_conf - 1 month ago 12
JSON Question

Invalid JSON formatting in Volley post request

I am trying to pass

data={"process" : "ExampleProcess", "phone" : "123456"}


As a post request in volley, thus far I have managed to create a(very basic) method that will send
the required information out to the server, but I am getting a failed response.

I added logs to the program for which I can see

ERROR:: com.android.volley.ParseError: org.json.JSONException: End of input at character 2 of

But I believe it has to do with the way I am trying to send my information over. Upon debugging I also came to realize that I was getting an error indicating

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.json.JSONObject.toString();

For which I do not know what to do, even if I change the:

total.put("data", data.toString());


to

total.put("data", data);


The error will persist. At this point I have no clue as to how can I go about sending the correct JSON post request to my URL(which has been taken out from the example btw)

public void volleyConnector(String url) {
final JSONObject data = new JSONObject();
final JSONObject total = new JSONObject();


try {

data.put("process", "ExampleProcess");
data.put("phone" , "123456789");
total.put("data", data.toString());

} catch(JSONException e) {

Log.v("JSON ERROR: ", e.toString());
}


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( url, total, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.v("--------------","---------------------------");
Log.v("RESPONSE: ", "-----> " + response.toString());
Log.v("<----- BTW------>", total.toString());
Log.v("--------------","---------------------------");

}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Log.v("--------------","---------------------------");
Log.v("RESPONSE: " , "xXxX FAIL X_____X FAIL XxXx");
Log.v("|>>>ERROR: ", error.toString());
//Log.v("|>>>ERROR: ", error.getLocalizedMessage());
Log.v("<----- BTW------>", total.toString());

Log.v("--------------","---------------------------");


} // end of onErrorREsponse
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}

};
Volley.newRequestQueue(this).add(jsonObjectRequest);
} // end of volley connector method


My complete error log(upon the failed request) reads:

V/RESPONSE:: xXxX FAIL X_____X FAIL XxXx
V/|>>>ERROR:: com.android.volley.ParseError: org.json.JSONException: End of i
V/<----- BTW------>: {"data":"{\"process\":\"ExampleProcess\",\"phone\":\"123456789\"}"}


I am not too sure as to what is going on and I am not getting anywhere by reading the docs and debugging with Android studio. As far as I am concerned, one of the parameters is a JSONObject and I am passing in one albeit it might be wrongly formatted.

Any help/pointers will be greatly appreciated.

Answer

You are doing correct but try code once by making one small change in your code that is-

JSONObject data= new JSONObject();

            data.accumulate("username", "mobileGps");
            data.accumulate("password", "9565551236");

            JSONObject total= new JSONObject();
            total.put("data",data);

            json = jsonObjectNew.toString(); 

And pass this "json" to method and check once. Looking same but try once.

Comments