I need to upload image to certain URL. Classic HTTP request building would look lik this:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
getString(R.string.WebServiceURL)
+ "/cfc/iphonewebservice.cfc?method=uploadPhoto");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("photoId", new StringBody(getIntent()
.getStringExtra("photoId")));
entity.addPart("returnformat", new StringBody("json"));
entity.addPart("uploaded", new ByteArrayBody(data,
"myImage.jpg"));
entity.addPart("photoCaption", new StringBody(caption.getText()
.toString()));
httpPost.setEntity(entity);
In short, suppose that what I need to do is to use httpPost.setEntity()
method to put an image into a request.
I already know how to build request using Volley library; one has to override getBody
method in com.android.volley.Request
class, for example like this:
public byte[] getBody() throws AuthFailureError {
JsonObject json = new JsonObject();
Set<String> keySet = mParameters.keySet();
for (String key : keySet) {
json.addProperty(key, mParameters.get(key));
}
return json.toString().getBytes();
}
Now the problem is how to set entity in Request
class? there's no method like setEntity()
.