E/Volley: [2342] NetworkUtility.shouldRetryException: Unexpected response code 400 - java

I'm using the Android volley library and I'm trying to sending data to the API server but it responds to this error every time
E/Volley: [2342] NetworkUtility.shouldRetryException: Unexpected response code 400 for API
This is my code:
JSONObject params = new JSONObject();
try {
params.put("deptcode", 649);
params.put("endDt", "2021-07-22T12:37:28.755Z");
params.put("instCode", 152);
params.put("instSesNO", 0);
params.put("locCode", 2);
params.put("observedBy", obserName);
params.put("sessionId", 0);
params.put("startDt", "2021-07-22T12:37:28.755Z");
params.put("status", "string");
}
catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, params,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
}
}) {
/**
* Passing some request headers
* */
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(300000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MyRequestQueue.add(jsonObjReq);
I tried many ways but still doesn't work for me. Please help me.

normally 400 is a server error that is something not found on server.
1- try your api on postman and verify it is working.
2- sometimes, the value we're passing in int can be resolved by converting them to string.
3- try to get the error message from error.getMessage().
if not resolved, please share your api i'll check.

Related

Android JSON request with Volley

I am trying to register some data with an api and I have checked the json that I get in my application and the structure and data are fine, I put it in Postman and it works fine, the url is also fine, someone could help me to know what I am doing wrong ?
this is the method with which I try to make the call to the api
private void request2(final String json){
// Instantiate the RequestQueue.
RequestQueue mRequestQueue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
// Request a string response from the provided URL.
StringRequest stringRequestPOSTJSON = new StringRequest(Request.Method.POST, BASE_URL + "/api/Asociado/RegistrarAsociado", new Response.Listener() {
#Override
public void onResponse(Object response) {
// response..
}
}, errorListener) {
#Override
public Priority getPriority() {
return Priority.HIGH;
}
#Override
public Map getHeaders() throws AuthFailureError {
HashMap headers = new HashMap();
headers.put("Authorization", "Bearer "+ bearertoken);
return headers;
}
#Override
public byte[] getBody() throws AuthFailureError {
String your_string_json = json; // put your json
return your_string_json.getBytes();
}
};
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequestPOSTJSON);
}
This is the error I get: E/Volley: [4146] BasicNetwork.performRequest: Unexpected response code 400 for
try this code:
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Android can not send post request with volley(BasicNetwork.performRequest: Unexpected response code 404)

I am trying to send some info to my api with post method(volley), but keep getting errors. Most consistent and the latest one is
E/Volley: [1449] BasicNetwork.performRequest: Unexpected response code 404 for http://....
D/Error.Response: com.android.volley.ClientError
I am able to post it with postman but not in android. I am sure nothing is wrong in the other side. I don't know what to do.
I have tried several implementations with getheader(),getBodyContentType(), Json object, string, parsing, try catch and so on..
RequestQueue queue = Volley.newRequestQueue(this);
String url = "....";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
Log.d("Error.Response", error.toString());
}
}
) {
#Override
public Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("email", "test#email.com");
params.put("token", "token");
return params;
}
};
queue.add(postRequest);

Android Volley unable to get data in response

I am trying to make a web-service call using volley and printing the response in the logcat. But I don't know why I am not getting response. Not even any error message.
Below is my code. I know I am missing something. Please correct me.
private void syncData() {
mProgressDialog.showProgressDialog("Initializing Please Wait...");
RequestQueue requestQueue = Volley.newRequestQueue(SalesDashboard.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, SYNC_DATA_SALES, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", error.toString());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String , String> params = new HashMap<>();
String userrole = mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE);
params.put("branch", mSessionManagement.getSelectedBranch());
params.put("staff_id", mSessionManagement.getLoggedInUser().get(SESSION_EMP_ID));
params.put("user_role", mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE));
params.put("user_dept", mSessionManagement.getLoggedInUser().get(SESSION_DEPT_IDS));
params.put("principal", mSessionManagement.getLoggedInUser().get(SESSION_PRINC_IDS));
if (userrole.equals(ADMIN) || userrole.equals(COUNTRY_MANAGER)) {
params.put("user_div", mSessionManagement.getSelectedDivision());
} else {
params.put("user_div", mSessionManagement.getLoggedInUser().get(SESSION_DIV_IDS));
}
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
I am not receiving any error message also or any kind of exception. I have checked the webservice by hitting it on browser the response is displayed correctly on the browser but not able to fetch it in android.
Why you call super.getParams() on return instead params? Maybe it turn wrong on your request. Then try as follow
#Override
protected Map<String, String> getParams() throws AuthFailureError {
...
return params;
}
Bonus: use getMessage instead do toString() your error object
Print the stringRequest and check the url and cross verify with ur url
Before that check have u added Internet permissions in manifest file.

WearOS cannot make Volley request to an endpoint running on my computer's localhost

Problem
I'm attempting to make a simple POST JsonObjectRequest from a Huawei Watch and I keep getting com.android.volley.TimeoutError. I can successfully make other POST JsonObjectRequests from the watch to a different endpoint (not on my computer's localhost) and I can successfully make requests to the desired endpoint from a laptop.
What I've Tried:
Most posts suggest that this error is due to a bad connection or an incorrect endpoint. I tried increasing the timeout period to see if my connection was just slow but this did not fix things. The endpoint is definitely correct as it works when I make the request from a laptop.
I read on several posts that there are issues when connecting to unknown certificates. I followed the quick fix outline here: https://newfivefour.com/android-trust-all-ssl-certificates.html but this made no difference
My Code
mRequestQueue.add(volleyRequest());
...
private JsonObjectRequest volleyRequest(){
JSONObject data = null;
try{
data = new JSONObject("{'some':'data','other':'data'}");
} catch (JSONException e){
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(POST, mPath, data,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i("Volley Response", response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("Volley Error", error.toString());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", mToken);
return headers;
}
};
return request;
}

AndroidVolley - Unexpected response code 500

Accessing a specific WEB-service using postman (a chrome extension for testing WEB services) results in a successful response, but my application implementation fails at hitting the same WEB-service and reports the following error:
E/Volley: [365] BasicNetwork.performRequest: Unexpected response code 500 for link I'm using
What am I missing here?
Thank you.
My application code:
private void Uploads(final String imagePath)
{
CustomJSONObjectRequest rq = new CustomJSONObjectRequest(Request.Method.POST, BASE_URL, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String success = response.getString("message");
Toast.makeText(getActivity(), success, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Response Error", error.toString());
Toast.makeText(getActivity(), "Fail", Toast.LENGTH_LONG).show();
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("token", "$2y$10$2V.Ux6CRmHoPCs2UziaVx.e6poDbFkZE2rrGrrLO1YbGcuUnkGFSS");
return headers;
}
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("email", "t#t.ru");
params.put("name", "Abyl");
params.put("phone", "7777777777");
params.put("fio", "fioasiodfaisfoiaosf");
params.put("zarplata_ot", "450000");
params.put("zarplata_do", "800000");
params.put("opit_raboty", "2");
params.put("city_id", "1");
params.put("img",imagePath);
params.put("user_id", "73");
params.put("description", "asdasdasdasdasdasdasdasd");
return params;
}
};
VolleyController.getInstance(getActivity()).addToRequestQueue(rq);
}
If you're using Volley , you have to check some of the basic configuration . So
Make sure , you have created a AppController class for volley.
Make sure , You initialize this Appcontroller in your Android Manifest.like
this.
<application android:name="app.AppController"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Print your params hashmap , your token and your stacktrace also .it'll help you to identify your problem in deep and check your Baseurl once again
Code 500 is a HTTP Error Code that means "Internal Server Error". So, probably the problem stays on your API's side

Categories

Resources