i am trying to change HTTPClient deprecated code but i get error on setEntity because its dapricated and i dont know how to paas MultipartEntityBuilder by new httpConnectionUrl. my old deprecated code snippt
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(imagePostUrl);
for (String fileName : fileNameArrayList) {
File file = new File(fileName);
// 1st
int quality = GeneralUtil.getQualityOfImage(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (quality <= 25) { // This means image file size is in MB's so we need to avoide out of memory issues.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
try {
bitmap = BitmapFactory.decodeFile(file.getPath(), options);
} catch (OutOfMemoryError E) {
System.gc();
bitmap = GeneralUtil.decodeFile(file);
}
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} else {
try {
bitmap = BitmapFactory.decodeFile(file.getPath());
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} catch (OutOfMemoryError E) {
System.gc();
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
bitmap = BitmapFactory.decodeFile(file.getPath(), options);
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} catch (OutOfMemoryError ex) {
bitmap = GeneralUtil.decodeFile(file);
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
throw new Exception();
}
}
}
byte[] data = bos.toByteArray();
String timeStamp = GeneralUtil.generateTimeStamp();
ByteArrayBody bin = new ByteArrayBody(data, myMobileNo + "_" + userName + "_" + timeStamp + ".jpg");
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("uploadedfile1", bin);
multiPartEntityBuilder.addPart("inviteId", new StringBody(inviteIdArrayList.get(0).toString(), Charset.forName("UTF-8")));
post.setEntity(multiPartEntityBuilder.build());
HttpResponse httpResponse = null;
httpResponse = client.execute(post);
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result.add(convertInputStreamToString(inputStream));
else
result.add("Did not work!");
}
my new not working code snippt
try {
HttpURLConnection httpcon = (HttpURLConnection) ((new URL(imagePostUrl).openConnection()));
httpcon.setDoOutput(true);
for (String fileName : fileNameArrayList) {
File file = new File(fileName);
int quality = GeneralUtil.getQualityOfImage(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (quality <= 25) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
try {
bitmap = BitmapFactory.decodeFile(file.getPath(), options);
} catch (OutOfMemoryError E) {
System.gc();
bitmap = GeneralUtil.decodeFile(file);
}
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} else {
try {
bitmap = BitmapFactory.decodeFile(file.getPath());
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} catch (OutOfMemoryError E) {
System.gc();
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
bitmap = BitmapFactory.decodeFile(file.getPath(), options);
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
} catch (OutOfMemoryError ex) {
bitmap = GeneralUtil.decodeFile(file);
bitmap.compress(CompressFormat.JPEG, quality, bos);
bitmap.recycle();
bitmap = null;
throw new Exception();
}
}
}
byte[] data = bos.toByteArray();
String timeStamp = GeneralUtil.generateTimeStamp();
ByteArrayBody bin = new ByteArrayBody(data, myMobileNo + "_" + userName + "_" + timeStamp + ".jpg");
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();
multiPartEntityBuilder.addPart("uploadedfile1", bin);
multiPartEntityBuilder.addPart("inviteId", new StringBody(inviteIdArrayList.get(0).toString(), Charset.forName("UTF-8")));
httpcon.setEntity(multiPartEntityBuilder.build()); **error**
// Execute POST request to the given URL
// HttpResponse httpResponse = null;
// httpResponse = client.execute(post);
httpcon.setRequestMethod("POST");
httpcon.connect();