How To Send Json Object To Server Using Volley In Android
I want to send the JSONObject to server using POST method . I have used volley library to pass the string params its working fine, but if i try to use json object its showing an e
Solution 1:
Third parameter in JsonObjectRequest is for passing post parameters in jsonobject form. And for header you need to send two separate values one for content-type one for charset.
RequestQueue queue = Volley.newRequestQueue(this);
privatevoidmakeJsonObjReq() {
showProgressDialog();
Map<String, String> postParam= newHashMap<String, String>();
postParam.put("un", "xyz@gmail.com");
postParam.put("p", "somepasswordhere");
JsonObjectRequest jsonObjReq = newJsonObjectRequest(Method.POST,
Const.URL_LOGIN, newJSONObject(postParam),
newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = newHashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setTag(TAG);
// Adding request to request queue
queue.add(jsonObjReq);
// Cancelling request/* if (queue!= null) {
queue.cancelAll(TAG);
} */
}
Solution 2:
Create new Method and call it in OnClick method
publicvoidPostOperation() {
requestQueue = Volley.newRequestQueue(this);
pdialog = newProgressDialog(this);
pdialog.setMessage("Loading");
pdialog.show();
StringRequest stringRequest = newStringRequest(Request.Method.POST, "YOUR_URL",
newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String response) {
pdialog.dismiss();
Log.e("login output", response);
//MODEL CLASSLoginModel loginModel = newGsonBuilder().create().fromJson(response, LoginModel.class);
if (loginModel.getStatus().toString().equalsIgnoreCase("true")) {
Intent i = newIntent(context, DashboardActivity.class);
startActivity(i);
finish();
Toast.makeText(context, loginModel.getStatus() + "", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, loginModel.getMsg() + "", Toast.LENGTH_SHORT).show();
}
}
},
newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
pdialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}) {
@OverrideprotectedMap<String, String> getParams() {
HashMap<String, String> map = newHashMap<String, String>();
// pass your input text
map.put("email" editTextEmail.getText().toString());
map.put("password",editTextPassword.getText().toString() );
map.put("uid", "1");
map.put("type", "login");
Log.e("para", map + "");
return map;
}
};
requestQueue.add(stringRequest);
}
Post a Comment for "How To Send Json Object To Server Using Volley In Android"