I send a XML
File via URL
to an PHP-Server.
Yesterday I changed the compiledSdkVersion from 17 to 23
compileSdkVersion 23
buildToolsVersion "23.0.2"
and after I rebuild the project it says me that the DefaultHttpClient
Method is deprecated.
This is the Code:
String sURL = "http://www.example.com/function?f=1000&";
String returnString;
File fileDir = new File(getFilesDir(), "job_active");
fileDir.mkdirs();
File file = new File(fileDir,filename);
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(sURL);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
//Füge die Datei zur Entity hinzu
multipartEntity.addPart("file", new FileBody(file));
//Füge den Dateinamen hinzu
multipartEntity.addPart("tmp_name", new StringBody(FilewithoutExt));
httpPost.setEntity(multipartEntity.build());
HttpResponse response = httpClient.execute(httpPost);
//Bekomme die Antwort vom Server zurück
returnString = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
return false;
}
Another deprecated Method is the StringBody
method.
I've seen an example in other Threads like:
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
But I can't find a way to replace those two methods. Can I use the HttpUrlConnection
and send a file with it?
I hope it's clear what my problem is.
Kind Regards!
Just add useLibrary 'org.apache.http.legacy'
into android
tag in your apps gradle file.
You can upload file using HttpURLConnection.
More details : http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106