I am trying to send an image to the server using HttpUrlConnection and MultipartEntityBuilder. The problem is that when I execute the background function, to send it and get as a response a String that says on the part of the server that has been sent successfully, the application crashea. The code is as follows:
public class EnvioImagenes extends AsyncTask<String, Void, String>
{
public String direccion="";
public EnvioImagenes(String cuerpo){
direccion=cuerpo;
}
protected String doInBackground(String... url){
Bitmap bitmap = null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
ContentBody contentPart = new ByteArrayBody(bos.toByteArray(), direccion);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Picture",contentPart);
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url[0]).openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
//Si quiero enviar/recibir una respuesta en el cuerpo del mensaje, tiene que estar lo siguiente:
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
String boundary= "--------------"+System.currentTimeMillis();
multipartEntity.setBoundary(boundary);
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
//connection.addRequestProperty(multipartEntity.getClass().getName(), multipartEntity.getClass().toString());
//OutputStream output=new BufferedOutputStream(connection.getOutputStream());
dos.writeBytes("\r\n");
dos.flush();
dos.close();
//output.write(body.getBytes());
//output.flush();
int responseCode = connection.getResponseCode();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder result = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
String responseString = result.toString();
inputStream.close();
connection.disconnect();
return responseString;
} catch(Exception exc) {
String error = exc.toString();
Log.e("Este es el error-----", exc.getMessage());
return error;
}
}
}
NOTICE: in order to use MultipartEntityBuilder I had to download an Apache library (doing what this question says link ), which throws me the following warning when I want to use it (although as I do not use HttpClient I do not think that influences that):
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.3 is ignored for release as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
This is the Build.gradle of the app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.example.franco.pruebalogin"
minSdkVersion 10
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'org.apache.httpcomponents:httpmime:4.5.3'
}