Throwing exception without repetition in multiplayer android
I am trying to upload an image file from my application to a server using the Multipart approach. Once I submit the request, I postpone the issue, named as "an exception without re-request". I am new to this approach and have no idea how to deal with this.
Here is my code: -
File imageFile = new File("/mnt/sdcard/link.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);
InputStream imageStream;
JSONObject objResult;
boolean bSucess = true;
// Base 64 image string was stored with image object ,
//String imageBase64 = image.getImageString();
// This base64 to byte , One can directly read bytes from file from Disk
String imageDataBytes = img_str.substring( img_str.indexOf(",")+1);
HttpClient client = null;
HttpPost post = null;
HttpResponse response = null;
HttpEntity httpEntity = null;
imageStream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
try{
//Forming Json Object
JSONObject jsonImageMetdata = new JSONObject();
JSONObject objMultipart = new JSONObject();
try {
objMultipart.put("status", 1+"");
objMultipart.put("type", "Photo");
objMultipart.put("filename", "menu.jpg");
objMultipart.put("filetype", "image/jpeg");
objMultipart.put("user_id", "1");
objMultipart.put("auth_id", "1");
objMultipart.put("userfile", Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
jsonImageMetdata.put("MultipartImageMetadata", objMultipart);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// converting json data to string
String strImageMetadata = jsonImageMetdata.toString();
client = new DefaultHttpClient();
post = new HttpPost("http://stage.phonethics.in/inorbitapp/place_api/menu_list");
post.setHeader("X-API-KEY", "d41d8cd98f00b204e9800998ecf8427e");
MultipartEntityBuilder entityBuilder = null;
try{
entityBuilder = MultipartEntityBuilder.create();
}
catch(Exception a){
Log.d("name",a.getMessage());
throw a;
}
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// adding text
entityBuilder.addTextBody("dummyParameter","Dummy text",ContentType.TEXT_PLAIN);
// adding Image
if(imageStream != null){
entityBuilder.addBinaryBody("file", imageStream,ContentType.create("image/jpeg"),"imagename.jpg");
}
// sending formed json data in form of text
entityBuilder.addTextBody("descriptions", strImageMetadata, ContentType.APPLICATION_JSON) ;
HttpEntity entity = entityBuilder.build();
post.setEntity(entity);
response = client.execute(post);
httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
Can someone help me figure this out. Any help would be appreciated. Thank.
source to share
My team hit the same issue this week. It turns out that use InputStream
for the body is the root cause of the problem. Since InputStream
it is not being read again, the request is marked as "Non repeatable". You need to use one of the other method signatures addBinaryBody
, such as the one that accepts java.io.File
.
source to share
I solved this problem by using the JetS3t Amazon toolkit class, its name RepeatableInputStream
. In your case, import this into your dependencies. I am using Ivy in my project. I will show you two modes.
Maven:
<dependency>
<groupId>net.java.dev.jets3t</groupId>
<artifactId>jets3t</artifactId>
<version>0.7.2</version>
</dependency>
Ivy:
<dependency org="net.java.dev.jets3t" name="jets3t" rev="0.7.2"/>
After using this class in your sample thread to port it as repeatable.
// adding Image
if(imageStream != null){
RepeatableInputStream imageStreamRepeatable = new RepeatableInputStream(imageStream,imageDataBytes.getBytes());
entityBuilder.addBinaryBody("file", imageStreamRepeatable,ContentType.create("image/jpeg"),"imagename.jpg");
}
source to share