Reputation: 99
I try to get response in my application, when I try to call API using volley library, it's giving com.android.volley.ServerError
and response code 400.
Here is my code
RequestQueue requestQueue = Volley.newRequestQueue(this);
try
{
//start bottom
String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails";
url = url.replaceAll(" ", "%20");
//String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/";
JSONObject jsonObject = new JSONObject();
jsonObject.put("EndUserIp","216.10.251.69");
jsonObject.put("TokenId","0307b931-bd7d-4860-9c4d-4d65103ebddc");
jsonObject.put("PNR","ZERD8U");
jsonObject.put("BookingId","1401272");
Log.i("JsonObject",jsonObject.toString());
JsonObjectRequest jsonobjectreq = new JsonObjectRequest(Request.Method.POST, url,jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.i("response",response.toString());
progressDialog.dismiss();
}
}
It is working in postman.The error i got
com.android.volley.ServerError
The Log is
10-12 10:34:19.525 7149-7189/com.farehawker E/Volley: [387] BasicNetwork.performRequest: Unexpected response code 400 for http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails
10-12 10:34:19.529 7149-7149/com.farehawker I/Error: com.android.volley.ServerError
The json I am sending is
{"EndUserIp":"216.10.251.69","TokenId":"0307b931-bd7d-4860-9c4d-4d65103ebddc","PNR":"ZERD8U","BookingId":"1401272"}
Postman request Postman response
Upvotes: 3
Views: 196
Reputation: 397
Replace your requestqueuw with below one:
RequestQueue requestQueue = Volley.newRequestQueue(this);
try {
String url = "http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails/";
url = url.replaceAll(" ", "%20");
JSONObject jsonObject = new JSONObject();
jsonObject.put("EndUserIp", "216.10.251.69");
jsonObject.put("TokenId", "0307b931-bd7d-4860-9c4d-4d65103ebddc");
jsonObject.put("PNR", "ZERD8U");
jsonObject.put("BookingId", "1401272");
Log.i("JsonObject", jsonObject.toString());
JsonObjectRequest jsonobjectreq = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("response", response.toString());
//progressDialog.dismiss();
}
},
// The final parameter overrides the method onErrorResponse() and passes VolleyError
//as a parameter
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
) ;
jsonobjectreq.setRetryPolicy(new DefaultRetryPolicy(
5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
jsonobjectreq.setShouldCache(false);
requestQueue.add(jsonobjectreq);
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 2
Reputation: 4087
AFAIK you're sending JsonObjectRequest to the server and for that, you need to pass Content-Type also with your Volley network call .
In your Volley NetworkCall Attach this header .
@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;
}
Upvotes: 2