I am trying to send an image to a server using MultipartEntityBuilder and HttpURLConnection, and then recieve a String answer (nowadays it uses http protocol but then I will do it with https using this code, or something very similar). But when I press the button to send it, the app crashes and the logcat doesn't tell me anything about the catch. The code from the class where I do it is the next one:
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("This is the mistake.....", exc.getMessage());
return error;
}
}
}
I downloaded the MultipartEntityBuilder library downloading it like this link says: Android - MultipartEntity and dependencies. The problem is that when the build.gradle checks if everithing is okey, it shows me this warning:
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.
I have tried to solve it downloading the package from the browser and then pasting it in the folder /lib inside the library, and then changing the code from the build.gradle to make the program checks the libraries from there, but then I had the mistake that the libraries in the code where I use the MultipartEntityBuilder weren't detected; so I think the problem is on the code itself: especially because I don't use httpClient. For any question, the code from build.gradle is actually this one:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
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'
}