Created
November 14, 2016 19:03
-
-
Save marosc/968b8e45639c54af94c923490a80c267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
RequestQueue requestQueue = Volley.newRequestQueue(this); | |
String URL = "http://..."; | |
JSONObject jsonBody = new JSONObject(); | |
jsonBody.put("Title", "Android Volley Demo"); | |
jsonBody.put("Author", "BNK"); | |
final String mRequestBody = jsonBody.toString(); | |
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 mRequestBody == null ? null : mRequestBody.getBytes("utf-8"); | |
} catch (UnsupportedEncodingException uee) { | |
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8"); | |
return null; | |
} | |
} | |
@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); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment