I want to upload multiple image at once. so I found 'MultipartEntityBuilder' but It is not working well.
It's my source...
public void executeMultipartPost() throws Exception {
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byteArray = byteArrayOutputStream.toByteArray();
builder.addTextBody("file1", byteArray.toString());
builder.addTextBody("file2", byteArray2.toString());
// send
InputStream inputStream = null;
HttpClient httpClient = AndroidHttpClient.newInstance("Android");
HttpPost httpPost = new HttpPost(UPLOAD_URL);
httpPost.setEntity(builder.build());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
// response
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
inputStream.close();
// result
String result = stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
When Debugging, 'httpPost.setEntity(builder.build())' this line is error.
and there is no 'Caused by~' ,but Process: NoSuchFiledError in logcat...
but I think I made image to bitmap then send it to FTP. what is wrong..? Thanks.
your code could get rid off the deprecated code of MultipartEntityand use MultipartEntityBuilder. However the specific problem related here is that the core Android libraries conflict with the newly added ones. Also now there are maven repositories available. You could try the following code into the below file:
build.grade (Module:app)
compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
Getting NoSuchFieldError INSTANCE org/apache/http/message/BasicHeaderValueParser