I want to changed a HttpClient to AsyncHttpClient . I want to do this because HttpClient is depreceted. Now I have probleme with send picture to server. I have a status code 400. This is my code:
File imagesFolder = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.i("HttpConUtil", imagesFolder.getAbsolutePath());
File file = new File(imagesFolder, photoName);
Log.i("HttpConUtil", file.getAbsolutePath());
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 50, bos);
byte[] data = bos.toByteArray();
String url = Util.getServerUrl(context) + "/AddFile";
ByteArrayBody bab = new ByteArrayBody(data, photoName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
RequestParams values = new RequestParams();
if (token) {
reqEntity.addPart("token", new StringBody(String.valueOf(E_Gps.TOKEN)));
values.put("token", String.valueOf(E_Gps.TOKEN));
} else {
reqEntity.addPart("uniq", new StringBody(Util.UNIQ));
values.put("uniq", String.valueOf(Util.UNIQ));
}
reqEntity.addPart("panels", bab);
reqEntity.addPart("filename", new StringBody(photoName));
values.put("panels", bab);
values.put("filename",String.valueOf(photoName));
AsyncHttpClient client = new AsyncHttpClient();
client.post(context, url, values, new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
callback.onSuccess(new String(responseBody));
Log.e("send photo !! " , statusCode + " " + new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
callback.failure(statusCode);
Log.e("send photo err !! " , statusCode + " " + new String(responseBody));
}
});
}
And this return status 400 And this is code for HttpClient and it is work fine:
File file = new File(filePath);
if (file.exists()) {
throw new FileNotFoundException(
"Plik o podanej nazwie nie istnieje!");
}
String name = file.getName();
byte[] data = org.apache.commons.io.FileUtils.readFileToByteArray(file);
HttpClient httpclient = HttpClientUtil.getHttpClient(context);
HttpPost httppost = new HttpPost(Util.getServerUrl(context)
+ "/AddFile");
ByteArrayBody bab = new ByteArrayBody(data, name);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("token", new StringBody(String.valueOf(E_Gps.TOKEN)));
reqEntity.addPart("ByteArrayBody", bab);
reqEntity.addPart("filename", new StringBody(name));
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
InputStream responseInputStream = new BufferedInputStream(response
.getEntity().getContent());
return responseInputStream;
}
logs:
05-18 15:12:19.628 12927-14178/arios.e_gps E/send photo err !!: 400 <html><head><title>Apache Tomcat/7.0.67 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Bledne dane</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Bledne dane</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.67</h3></body></html>