Android Volley: Unexpected response code 405 - android

My Android Volley JsonObjectRequest runs into onErrorResponse with the issue:
BasicNetwork.performRequest: Unexpected response code 405 for MY_URL
My URL is valid. I have checked that with a browser and
I get there the expected JSON Object.
So the issue has to be on the client side.
The code 405 means:
Method Not Allowed The method specified in the Request-Line is not
allowed for the resource identified by the Request-URI. The response
MUST include an Allow header containing a list of valid methods for
the requested resource.
my code for JsonObjectRequest:
JsonObjectRequest jsonReq;
jsonReq = new JsonObjectRequest(URL_FEED, new JSONObject(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.v("ERROR:%n %s", error.getMessage());
}
});
// Adding request to volley request queue
NetworkController.getInstance().addToRequestQueue(jsonReq);
Do I have to add some information to the header? And if what for information?

The issue was that the request was set to POST by default.
The solution that worked for me:
JsonObjectRequest jsonReq = new JsonObjectRequest
(Request.Method.GET, URL_FEED, null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
Log.d("Server", "Läuft");
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Log.d("Server","onErrorResponse");
}
});
NetworkController.getInstance().addToRequestQueue(jsonReq);

Use GET Method instead of POST it has worked for me.

I had the same issue, and I found out that my API URL was wrong.
So, my suggestion is to recheck the API URL.

Related

JSONObject response null

I'm trying to send a simply JSON request using Volley JsonObjectRequest.
After getting the JSON response, I would like to update a value called valoreConvertito, but the JSON response is null and consequently valoreConvertito remains zero.
private void convertiREST(final Double valoreDaConvertire, String valuta){
final TextView textView = (TextView) findViewById(R.id.text);
RequestQueue queue = Volley.newRequestQueue(this);
String url =COMPLETEURL;
valoreConvertito = 0.0;
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.e("Rest response ", response.toString());
valoreConvertito = response.getJSONObject("quotes").getDouble("valuta");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest response", error.toString());
}
});
queue.add(objectRequest);
}
I've even followed the suggestions in another post (Volley JsonObjectRequest response) but I still got JSON response null.
Using the debugger it seems that the program doesn't enter neither in onResponse nor in ErrorListener.
After adding the row queue.add(objectRequest); I notice in Logcat the HTTP traffic not permitted error and I've solved my issue following Android 8: Cleartext HTTP traffic not permitted post.

Cannot cast 'com.android.volley.ServerError' to 'com.android.volley.NoConnectionError'

URL: http://androidtutorialpoint.com/api/volleyJsonObject
Code:
public void volleyJsonObjectRequest(String url) {
String REQUEST_TAG = "JSONOBJ_TAG";
JsonObjectRequest jsonObjectReq = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY RESPONSE", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
VolleyLog.d("VOLLEY ERROR", "Error: " + error.getMessage());
}
});
// Adding JsonObject request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectReq, REQUEST_TAG);
}
I am trying to log the volley respone but I am getting the following error instead:
Cannot cast 'com.android.volley.ServerError' to 'com.android.volley.NoConnectionError'
I have checked the URL in the POSTMAN and it works fine. Is there something I am missing in my code? Been trying to debug but couldn't find the root cause.
Found the solution:
Replace http with https

Why isn't Volley isn't returning any information from an API?

I'm unable to retrieve JSON from the TFL(Transport for London) API using Volley. I've tested my code with a different API and all works perfectly. The API URL is working fine - I have tested the URL with Postman. Logcat isn't showing and red errors.. Please see code & logcat below.
tempTextView = (TextView) findViewById(R.id.tempTextView);
String url = "https://api.tfl.gov.uk/Line/Victoria";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// tempTextView.setText("Response: " + response.toString());
Log.e("status", "Response: " + tempTextView.toString());
try {
JSONObject statusJSONObject = response.getJSONObject("lineStatuses");
String status = statusJSONObject.getString("statusSeverityDescription");
tempTextView.setText(status);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
// Access the RequestQueue through your singleton class.
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsObjRequest);
}
logcat (UPDATED)
10-31 19:24:10.410 18618-18618/com.example.aaron.itube E/TAG: Error response:
com.android.volley.ParseError: org.json.JSONException: Value [{"$type":"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities","id":"northern","name":"Northern","modeName":"tube","disruptions":[],"created":"2017-10-31T10:48:22.99Z","modified":"2017-10-31T10:48:22.99Z","lineStatuses":[{"$type":"Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities","id":0,"statusSeverity":10,"statusSeverityDescription":"Good Service","created":"0001-01-01T00:00:00","validityPeriods":[]}],"routeSections":[],"serviceTypes":[{"$type":"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities","name":"Regular","uri":"/Line/Route?ids=Northern&serviceTypes=Regular"},{"$type":"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities","name":"Night","uri":"/Line/Route?ids=Northern&serviceTypes=Night"}],"crowding":{"$type":"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"}}] of type org.json.JSONArray cannot be converted to JSONObject
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)
Caused by: org.json.JSONException: Value [{"$type":"Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities","id":"northern","name":"Northern","modeName":"tube","disruptions":[],"created":"2017-10-31T10:48:22.99Z","modified":"2017-10-31T10:48:22.99Z","lineStatuses":[{"$type":"Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities","id":0,"statusSeverity":10,"statusSeverityDescription":"Good Service","created":"0001-01-01T00:00:00","validityPeriods":[]}],"routeSections":[],"serviceTypes":[{"$type":"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities","name":"Regular","uri":"/Line/Route?ids=Northern&serviceTypes=Regular"},{"$type":"Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities","name":"Night","uri":"/Line/Route?ids=Northern&serviceTypes=Night"}],"crowding":{"$type":"Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"}}] of type org.json.JSONArray cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.(JSONObject.java:160)
at org.json.JSONObject.(JSONObject.java:173)
at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123) 
Thanks for looking.
For start write some code in the onErrorResponse method to log the errors received like the following:
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "Error response:", error);
}
Then update and post the errors in your question.
UPDATE
From the updated logcat here is the clue of type org.json.JSONArray cannot be converted to JSONObject. The response you receive is of type json array and not json object. So change your call to:
JsonArrayRequest jsArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
// place your code here, mind now this is a `JSONArray` and not a `JSONObject`
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "Error response:", error);
}
});
// Access the RequestQueue through your singleton class.
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsArrayRequest);

JsonObjectRequest error

I'm getting an error for Request.Method on the "Method" part and it says "Cannot resolve symbol"and I don't know why. I'm trying to parse JSON to pull images from a website.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, getRequestUrl(10),
(String) null, new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
L.zT(this, response.toString());
}
}, new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
}
});
You probably have the wrong import. Check in your import declaration of Request, and make sure that is
com.android.volley.Request
and not something else

Could not get response using volley lib

I got stuck with Volley library, any one please help me out.
The question is, I'm using Volley library for API call, it works when I call in a traditional method. I tried to use Volley library for better response but I'm not getting any response, some times I get the response but many times it fails. My snippet is:
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
parseJSON(response);
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new VolleySearchResultAdapter(searchResults, mImageLoader);
lstView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG,error.getMessage());
}
});
mRequestQueue.add(jr);
And how do I do pagination using this library?

Categories

Resources