Volly is giving me com.android.volley.AuthFailureError in Android - android

CODE
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjectRequest);
PROBLEM
This is my code in which I'm using the Volly-Library to get the JSON Object using the GET Method. I'm using Volly Library in my Android App to get the Json response using the Volly-Library. But it is throwing the AuthFailureError.

Related

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

volley code onResponse() and onErrorResponse() are not called

Here is my volley code, but onResponse() as well as onErrorResponse() are not called. The Log lines are never printed as these metods are skipped. But the last log line appears in logcat and the jsObjRequest is always null.
String url = "A valid url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("onResponse",""+response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", "" + error);
}
});
Log.e("jsObjRequest", "" + jsObjRequest);
You need to enqueue your request:
RequestQueue queue = Volley.newRequestQueue(this);
String url = "A valid url";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("onResponse",""+response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", "" + error);
}
});
Log.e("jsObjRequest", "" + jsObjRequest);
queue.add(jsObjRequest);
see this: https://developer.android.com/training/volley/simple.html
You should add your request to a RequestQueue. In your situation simply add these 2 lines of code below yours, it will work then.
RequestQueue rQueue = Volley.newRequestQueue(this);
rQueue.add(jsObjRequest);
For this issue i use HTTPS and its working very well now

How to get JSON from server without send JSON request

I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?
till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}
Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);

How to make a Json Array Request with Request Parameters

I have to make json array request with the following request paramatetrs.
[
"Login",
{
"password": "",
"username": "",
"ip": "12.123.124.12",
"login_type": "Android"
}
]
I use volley to make post request. In volley, if we have to make jsonarrayrequest, we do something like the following,
JsonArrayRequest req = new JsonArrayRequest(Constants.requestUrl,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
The problem is that, How can I ever be able to insert my request parameters in the above code snippet. In case of JsonObjectRequest, we have provision to insert as follows,
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.requestUrl, frameLoginJson(),
new Response.
Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
In frameLoginJson, im framing the request parameter and dispatching the request.
But Im unable to do the same in case of JSONArrayRequest. How can I be able to make json array request with request parameters using volley especially or by any other mean?
You would need to override the getParams method with the following signature: protected Map<String,String> getParams().
Here's an example:
see the answer to Volley not calling getParams() for standard POST request

JsonObjectRequest timeout error, but StringRequest works fine

I have an android application that requests a JSON object to a REST Service (ASP.NET MVC). I'm using Volley to do this.
The code for doing this:
RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest (
Request.Method.GET,
"http://192.168.1.253:8090/MyController/MyAction?myParameter=5B1C084B-EFE1-4292-BAF4-A2C30126171D",
null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getActivity(), "OK!" + response.toString(), Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null) {
Toast.makeText(getActivity(), "Error Response code: " + error.networkResponse.statusCode, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Error: " + error.toString(), Toast.LENGTH_SHORT).show();
}
mProgressDialog.dismiss();
}
});
requestQueue.add(request);
mProgressDialog = ProgressDialog.show(getActivity(), "My App", "Searching");
When I debug, the error is a TimeoutError instance.
Now, when I switch from JsonObjectRequest to StringRequest, the request works fine, and returns the JSON string, as expected.
The json that the service is returning is:
[{"Id":"0f376a18-f311-4bc3-b236-a2c3012bbbe1","Name":"HiAll","Route":[{"Order":0,"Latitude":24.207535461007971,"Longitude":65.26852548122406,"Stop":false,"Flag":true},{"Order":1,"Latitude":24.208240008486147,"Longitude":65.26927649974823,"Stop":true,"Flag":true},{"Order":2,"Latitude":24.209531668746457,"Longitude":65.270671248435974,"Stop":false,"Flag":true}]}]
Am I missing something?

Categories

Resources