在Android中不推荐使用DefaultHttpClient和StringBody

时间:2015-12-03 08:39:09

标签: php android apache

我通过XMLURL文件发送到PHP服务器。

昨天我将compiledSdkVersion从17改为23

compileSdkVersion 23
buildToolsVersion "23.0.2"

在我重建项目后,它告诉我{@ 1}}方法已被弃用。

这是代码:

DefaultHttpClient

另一个不推荐使用的方法是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; } 方法。

我在其他主题中看过一个例子:

StringBody

但我无法找到替代这两种方法的方法。我可以使用URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); 并随身携带文件吗?

我希望它清楚我的问题是什么。

亲切的问候!

2 个答案:

答案 0 :(得分:0)

只需将useLibrary 'org.apache.http.legacy'添加到应用程序gradle文件中的android标记中即可。

答案 1 :(得分:0)