I want to get the count of the documents in remote mongodb database. For that I am using custom query url. The url only returns an integer instead of a JSON packet. I am using Volley in android to make this query. The following code gives Error: E/Error﹕ com.android.volley.ParseError: org.json.JSONException: Value 2 of type java.lang.Integer cannot be converted to JSONObject
JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
Log.d("onResponse", jsonObject.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error",volleyError.toString() );
}
});
The request made using the url in a browser gives the following result.
It's not a JSON Request Use String request so it will return the response as a 2.
so simple you can get this using
int value = yourjsonobject.getInt("key");
If it helps please let me know
Related
My Restful API expect a list of integer arrays. The API works fine when I use postman client to send an json array like so [1,3,4].
I am using android volley to make requests. I have a list of integers which I want to send to the API like this.
//converting integer array to a json array
JSONArray myIntegerJsonArray = new JSONArray(myIntegerArray);
String url = "myurl...";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, myIntegerJsonArray, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//... do stuff here
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
MySingletonRequestQueue.getInstance(this).addToRequestQueue(request);
I am currently getting HttpMessageNotReadableException: Required request body is missing error. So I am guessing it's not sending the myIntegerJsonArray for some reason. What am I doing wrong?
I am getting the following error from a webapi call in an Android app using volley:
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
And I want to know how to see the exact message from the server as opposed to the Volley error. Here is the code:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: " + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error instanceof ParseError) {
Log.d(TAG, "onErrorResponse: ParseError: ");
}
}
});
queue.add(request);
It is a really basic call and under most circumstances I get back the jsonobject, but in those cases where the server sends me something different I want to be able to account for it. The error which is thrown is parsing of a value which is not json because it is an error. How can I see what the server is sending from the ErrorListener?
I am learning about Volley and I don't know why the response from GET method is coming as a single char -> [.
I am using this method to get the JSON response:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "https://www.w3schools.com/js/myTutorials.txt";
String url = "http://www.google.com"; // with this url I am getting response
// Request a string response from the provided URL.
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Response is: " + response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Response is not good" + error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
When I am using this link I do get a response but when I try to use some link that contains nothing but JSON like this one my response it "[".
I am calling this method from Activity like this:
GetJsonClass getJson = new GetJsonClass(this);
getJson.getJsonMethod();
Any ideas on what am I doing wrong here?
Answer + code
If anyone will start using Volley maybe this can help him :
as David Lacroix said in his answer, I called stringRequest and notJsonArrayRequest.
Here is how it should have been:
public void getJsonMethod() {
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
String url = "your url";
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
System.out.println("this is response good" + response);
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("this is response bad" + error);
}
});
queue.add(jsonObjectRequest);
}
See https://developer.android.com/training/volley/request
StringRequest. Specify a URL and receive a raw string in response. See Setting Up a Request Queue for an example.
JsonObjectRequest and JsonArrayRequest (both subclasses of JsonRequest). Specify a URL and get a JSON object or array (respectively) in response.
You should be using a JsonArrayRequest
myTutorials.txt is being served with status code 304 (no proper suffix and MIME type either):
304 Not Modified. If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
In other terms, what the browser may display is not neccessarily the same what the server has sent. eg. GSON would accept that JSON only with option lenient enabled, because the array has no name.
see RFC 2616.
I want to send parameters such as username and password.
I got an error like String cannot be converted to jsonobject.
I dont know what this happening.Anyone pls help me my code is:
JSONObject obj=new JSONObject();
try{
obj.put("username","test");
obj.put("password","test");
} catch (JSONException e) {
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
urlJsonObj, obj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,json_obj_req);
}
There is nothing wrong with the way you are creating JSONObject and putting values in it. Make sure the response received is Json, because your onResponse method accepts JSONObject. You could be receiving String value as response, which could not be converted to JSONObject.
It looks like your response is actually a string and not a json object i.e. {"object":"value"} but rather "object:value". You need to sniff your response via either Stetho, Fiddler or reenact your request via Postman (or Fiddler)
======================
This doesn't answer your question, but this will help you tremendously and make your life easier.
Highly recommend using Gson and Retrofit to make HTTP requests and parse Gson objects easily.
https://github.com/google/gson
http://square.github.io/retrofit/
Hi guys i am facing a problem and the problem is that i am trying to send post request to server and i am getting exception
com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
when i try to send data through get request it is working properly and when i try to send data through post request i am getting the above exception i have tried searching but not succeeded.
below is my code
JsonObjectRequest _send_cab_data_to_server = new JsonObjectRequest(Request.Method.POST,
cab_Data_Url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("", "response : " + response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("", "error : " + error.toString());
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Method", "CarLocation");
params.put("data", _Cab_Locations_In_Driver_Service);
return params;
}
};
// Adding request to request queue
Volley_Controller.getInstance().addToRequestQueue(_send_cab_data_to_server,
"volley");
}
and the data attribute corresponds to a set of strings that are combined
e.g Ï2|33.7129221|73.0634634|Apr28,201510:50:33AM|0ÎÏ2|33.7129272|73.0634653|Apr28,201510:50:58AM|0Î
and the json response is
{
"Success": true,
"Info": "Successful",
"Response": [
{
"Status": "Done"
}
]
}
when i try to send data through get request it is working properly and
when i try to send data through post request i am getting the above
exception
As you said you are getting below exception:
com.android.volley.ParseError: org.json.JSONException: End of input at character 0 of
This means that you are getting null response. You are supposed to use GET method only, not the POST method because your server is not responding to the post request but rather returning a null object and so your parser is getting failed at parsing null object.
FYI, There is a meaning and importance of GET and POST method. Use the appropriate method defined by your server API.