[英]How to use POST method to send raw JSON using Retrofit in android
[英]How to send post request using raw json with volley or retrofit in android
public void login() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "https://api-test01.moneyboxapp.com/users/login";
final String requestBody = "{\"Email\":\"androidtest@moneyboxapp.com\",\"Password\":\"P455word12\",\"Idfa\":\"ANYTHING\"}";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("AppId", "3a97b932a9d449c981b595");
params.put("Content-Type", "application/json");
params.put("appVersion","5.10.0");
params.put("apiVersion","3.0.0");
return params;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.