Reputation: 33
I am trying to Post Json to my restful API with volley but it doesn't work. So far i have tested my web service by sending a json payload through the Advance rest client app on chrome and it returns a json response.... but when i try it with Volley it returns onErrorResponse. Please can someone tell me how to solve this problem, thanks in advance.
Json payLoad:
{"country":"isreal","mobile":"009988778"}
Code
private void processLogin() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.LOGIN_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("JSON response", "JSON Posting" + response.toString());
hideProgessDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
hideProgessDialog();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams(){
String country_value = country.getSelectedItem().toString();
String mobile_value = mobile.getText().toString();
Map<String, String> params = new HashMap<>();
params.put("country",country_value);
params.put("mobile", mobile_value);
return params;
}
};
AppController.getInstance().addToRequestQueue(jsonObjReq, login_tag);
}
Upvotes: 0
Views: 1407
Reputation: 1
Override getBodyContentType to set as json
Gson gson = new Gson();
final String json = gson.toJson(loginDTO);
GsonRequest<EmailLoginRestResponse> jsObjRequest = new GsonRequest<EmailLoginRestResponse>(
Request.Method.POST, url,
EmailLoginRestResponse.class, null,
this.createLoginRequestSuccessListener(),
this.createLoginErrorListener()){
@Override
public byte[] getBody() throws AuthFailureError {
return json.getBytes();
}
@Override
public String getBodyContentType() {
return "application/json; charset=" + this.getParamsEncoding();
}
};
jsObjRequest.setShouldCache(false);
this.mRequestQueue.add(jsObjRequest);
GsonRequest.java
Upvotes: 0
Reputation: 9234
What you are doing is, you are trying to send a JSON as a part of Headers which won't work.
This is what you need -
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.LOGIN_URL, **->YOUJSONOBJECTHERE<-**, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("JSON response", "JSON Posting" + response.toString());
hideProgessDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
hideProgessDialog();
}
});
You need to send JSONObject
as a part of request not as a part of request headers. Try it out and let me, if it fixes the issue else we troubleshoot it.
And as you already using JSONObjectRequest
, you niether need to set the content type nor override getParams()
until or unless you need to send some extra information in your header like AgentType, tokens etc.
Upvotes: 5