Android Volley EOFException - android

I have a simple app that uses volley like this:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("VolleyController", "Received volley response: " + response);
callback.onCallback(OperationOutcome.SUCCESS, response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// This is null.
NetworkResponse response = error.networkResponse;
Log.e("VolleyManager", "Received error response from volley: ", error );
callback.onCallback(OperationOutcome.FAILURE, null);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
This is the exception I get:
Received error response from volley: com.android.volley.NoConnectionError: java.io.EOFException
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112) 
I've found some other answers about this online but they didn't solve it. The request is not getting to the server.
Thank you very much.
EDIT
I have both the permission on the manifest:
<uses-permission android:name="android.permission.INTERNET" />
and Internet is working fine on the device.

Do you have your internet enabled on your device?
Did you set the INTERNET permission in your manifest?
EDIT:
Check your if your URL is correct.

The problem was due to the fact that the request I was sending was empty. I thought that Volley would convert it to the correct JSON representation({}) by itself. Instead, I think the request was just a null and this caused the exception. To fix this, I've changed the StringRequest in a JSONObjectRequest. This has a constructor which takes as an input a JSONObject like this:
// This will instantiate an empty JSON request with the correct format.
new JSONObject("{}");
I still don't get why Volley doesn't do by itself and I'd like to understand just out of curiosity. Anyway, after this, I've decided to switch to Retrofit which is much more performant and simpler to set up.
Thank you everybody for your help!

Related

Volley throw 502 bad gateway

I'm using Volley to send Get method.Sometimes I have 502 bad gateway server error.I don't know what is a wrong or how i can solve my problem
This is a source code
public void polygonRequest(final String userID, final String secretKey) {
showpDialog("loading");
polygonModelList.clear();
String url = ServerUtil.polygon+userID+"/"+secretKey;
final JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
SavedPolygonJson polygonModel=new SavedPolygonJson();
polygonModel.setPolygonJson(response.toString());
polygonModel.saveInStorage();
parsePolygonRequest(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hidepDialog();
showOneItemDialog(error.toString(),false);
}
});
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
CoreApplication.getInstance().getRequestQueue().add(jsObjRequest);
}
When I send my request with browser i have not any errors.How i can fixed this error...
How i can combine browser's request header with Volley request header?
thanks
Increase the the read time out session if you are using the retrofit.
Or in case of volley in crease the request time session from its default
for cross check hit the url directly in web browser and calculate the time that how much time it takes to fetch the response according to that you set the request session time
This happens because if your network lib does not get the response on predefined request time it show bad gateway 502

No authentication challenges found - Volley

Hi I am using volley to parse my api, when i pass the valid credentials it works fine gives me ab auth token and if the credentials are wrong i still want it to parse the response but its giving me "No authentication challenges found" and JSONObject response as null response in return. Is it something with volley or it is something with the php script??
private void sendRequest_getAuth(){
try {
JSONObject jsonBody = new JSONObject();
jsonBody.put("phone", "phn in numeric");
jsonBody.put("password", "pswd");
JsonObjectRequest JSONRequest = new JsonObjectRequest(Request.Method.POST, "the api url", jsonBody,
new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
jresponse=response;
show_getAuth(jresponse);
}
},
new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("onError_response",jresponse.toString());
Toast.makeText(LoginActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
show_getAuth(jresponse);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(JSONRequest);
}
required Response which should be parsed:-
{
"errors": [
{
"message": "Invalid Credentials"
}
]
}
Try the following thing:-
Check you Url in Postman (or any applicaiton) and pass invalid credentials and check what response you are receiving from webservice. If you are not getting respose as you have mentioned in you question then its Server side issue (PHP).
Now if you are getting response as above, the kindly check if successfull login and error in login response pattern is same.
Because if the json signature mismatches it won't be able to parse.
If its entering in Error callback kindly mention what is the error server is returning.
Have look at this link too...
java.io.IOException : No authentication challenges found
In case of any doubts, feel free to reply...

Get HttpResponse object as response from Volley api in android not jsonobject or string [duplicate]

This question already has an answer here:
How to get the JSONObject server response in Volley
(1 answer)
Closed 6 years ago.
Hi i am using android studio and for network call using volley api.
Since it's all good to get JsonObject and String as response from network call .
Is it possible to get HttpResponse from volley api ? or any method by which we can retrieve HttpResponse ?
Since i have to use external jar, in which method i can get HttpResponse object as input parameter.
Also we are getting NetworkResponse object from volley api , can we get HttpResponse from it ?
Although I am not totally clear with the question or the problem you have been facing .
Let me guess you want the Http response from the request and post it to server with another API call ? if this is your problem , Simple solution is to override parseNetworkResponse in makeStringReq()
StringRequest strReq = new StringRequest(Method.GET,
Const.URL_STRING_REQ,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
int mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
then you can post the mStatusCode to server .
if I am wrong what i guessed just let me know . **If there is a will , there is a way ** , you know :)

Android Volley JsonObjectRequest returns same response every time on mobile data

I am using Volley JsonObjectRequest to get data from server.
code snippet:
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println("Response: " + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
But I am getting same JSONObject response every time on mobile data connection.
Note: It's work perfectly on WiFi connection.
Is anyone facing this issue ? any solution ?
#BNK request.setShouldCache(false); worked for me. It's issue of volley cache management.
I assume that, when a request is sent:
It would hit the cache first and send that to onResponse
then when the results come through from the remote server it would provide it to the onResponse
If you use any of the default Request classes implemented in volley(e.g. StringRequest, JsonRequest, etc.), then call setShouldCache(false) right before adding the request object to the volley RequestQueue
request.setShouldCache(false);
myQueue.add(request);
You can also set expiration policy for cache.
See this answer for more details

Volley - no response from the URL

I am unable to get any response from the URL I am hitting using the Volley library, please can you help me out ?
I am getting this on my emulator
: "com.android.volley.NoConnectionError:java.io.IOexception:content length promised 45 bytes, but received 0 ."
I have pasted the code below:
String url = "http://ipchicken.com"
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.POST, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
sampletext.setText("Response => " + response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
sampletext.setText( error.toString());
}
});
queue.add(jsObjRequest);
If you constructed your RequestQueue by hand, instead of calling Volley.newRequestQueue, you need to call .start() on it. Otherwise it looks fine.
Thanks
In addition to what has been answered above: The url does not return any JSONObject, hence why you do not see any response [Your request is that of a JSONObject, meaning Volley expects JSONObject back as a response]. Maybe use the StringRequest instead.
Why you use POST method? If you need exactly POST, where are params?
I think, you should use GET method and parse request to find your name address or something else.
You most likely need to include the INTERNET uses-permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />

Categories

Resources