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?
Related
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.
I'm getting a Null Exception with JSon object from Volley
I filled the JSon object before it is used, and not in the parameterlist.
public static void SendPost6(final Context context){
final String TAG= "-->Error-->";
String url = "http://192.168.44.120/test_php_neuer_user.php";
RequestQueue queue = Volley.newRequestQueue(context);
JSONObject userObject = new JSONObject();
JSONObject paramsObject = new JSONObject();
try {
paramsObject.put("name", "Name");
paramsObject.put("email", "EMail");
userObject.put("user",paramsObject);
}
catch (JSONException e){
Toast.makeText(context, "JSON-Error:" + e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
userObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(context, "Volley Response:" + response.toString(), Toast.LENGTH_LONG).show();
}},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//handle errors#
Log.d(TAG, "Failed with error msg:\t" + error.getMessage());
Log.d(TAG, "Error StackTrace: \t" + error.getStackTrace());
Toast.makeText(context, "Volley Error:" + error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
try {
byte[] htmlBodyBytes = error.networkResponse.data;
Log.e(TAG, new String(htmlBodyBytes), error);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
queue.add(request);
//AppController.getInstance().addToRequestQueue(request);
I want to add the json object request into the queue to perform http-post. Volley Error is "null"
The firewall blocked the network access. This is why i'm getting response null. Solved.
I am hitting API on the server. It is working perfectly but when I run it in my app I am getting volley onErrorResponse along with data . I am not able to understand how it is possible. I am sharing code and error image. Please check.
private void callProductsApi() {
String tag_json_obj = "json_obj_req";
String url = Constants.GET_PRODUCTS;
pBar.setVisibility(View.VISIBLE);
JSONObject params = new JSONObject();
try {
params.put("token", Constants.token);
} catch (Exception e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, params,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.e("login_response", response.toString());
pBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: " + error.getMessage());
Toast.makeText(Splash.this, R.string.some_error_occured, Toast.LENGTH_LONG).show();
pBar.setVisibility(View.GONE);
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
100000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
The api should return success code which is 200, 201 and so on response. if it returns
other response in line of 400 or 500 it will display the correct json but in error method.
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
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);