Android Volley Posting Binary Body
Scenario - upload binary data in the body of a post, handle a response body containing JSON. How to do the following using Volley? curl -X POST -H 'X-Application-Id: 3KxPB' -H
Solution 1:
To send binary data you can do something like what I did in this answer How to send a “multipart/form-data” POST in Android with Volley .
Solution 2:
I was able to solve this using a Volley GsonRequest:
publicclassMainActivityextendsAppCompatActivity {
Stringurl="https://arcane-anchorage-34204.herokuapp.com/handleCode";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObjectjsonBody=null;
try {
jsonBody = newJSONObject ("{\"code\":\"NZ4UBUB\"}");
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error e = " + e, Toast.LENGTH_SHORT).show();
}
Map<String, String> headers = newHashMap<String, String>();
headers.put("Content-Type", "application/json");
RequestQueuequeue= Volley.newRequestQueue(this);
GsonRequest<Routine[]> gsonRequest = newGsonRequest<Routine[]>(Request.Method.POST, url, Routine[].class, headers, newResponse.Listener<Routine[]>() {
@OverridepublicvoidonResponse(Routine[] routineData) {
TextViewserverData= (TextView)findViewById(R.id.serverData);
Stringcomplete="";
Stringrepeat="";
Stringhold="";
Stringperform="";
StringtxtData="";
for (inti=0; i < routineData.length; i++) {
complete = (routineData[i].instructions.complete != null) ? "Complete: " + routineData[i].instructions.complete : "";
repeat = (routineData[i].instructions.repeat != null) ? "Repeat: " + routineData[i].instructions.repeat : "";
hold = (routineData[i].instructions.hold != null) ? "Hold: " + routineData[i].instructions.hold : "";
perform = (routineData[i].instructions.perform != null) ? "Perform: " + routineData[i].instructions.perform : "";
txtData += "DESCRIPTION: " + routineData[i].description[0] + ": " + routineData[i].description[1] + ", " + complete + ", " + repeat + ", " + hold + ", " + perform + " ";
}
serverData.setText("Response: " + txtData);
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError volleyError) {
TextViewserverData= (TextView)findViewById(R.id.serverData);
serverData.setText("Response: " + volleyError.toString());
}
}, jsonBody);
queue.add(gsonRequest);
}
publicclassGsonRequest<T> extendsRequest<T> {
privatefinalGsongson=newGson();
privatefinal Class<T> clazz;
privatefinal Map<String, String> headers;
privatefinal Response.Listener<T> listener;
privateJSONObjectparameters=null;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
* @param headers Map of request headers
*/publicGsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
publicGsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers,
Response.Listener<T> listener, Response.ErrorListener errorListener, JSONObject parameters) {
this(method, url, clazz, headers, listener, errorListener);
this.parameters = parameters;
}
@Overridepublic Map<String, String> getHeaders()throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Overridepublic String getBodyContentType() {
return"application/json";
}
@Overridepublicbyte[] getBody() throws AuthFailureError {
try {
return parameters.toString().getBytes(getParamsEncoding());
} catch (UnsupportedEncodingException e) {
}
returnnull;
}
@OverrideprotectedvoiddeliverResponse(T response) {
listener.onResponse(response);
}
@Overrideprotected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
Stringjson=newString(
response.data, HttpHeaderParser.parseCharset(response.headers));
Log.i("RESPONSE", json);
return Response.success(
gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(newParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(newParseError(e));
}
}
}
}
Post a Comment for "Android Volley Posting Binary Body"