我正在將應用升級到API 23,其中org.apache.http
已棄用。org.apache.http.entity.FileEntity在Android 6(棉花糖)中已棄用
我現在的(不建議使用)的代碼如下所示:
HttpClient httpClient = new DefaultHttpClient();
File file = new File(attr.Value);
String url = server_url;
HttpPost request = new HttpPost(url);
FileEntity fileEntity = new FileEntity(file, "image/png");
request.setEntity(fileEntity);
HttpResponse response = httpClient.execute(request);
String output = getContent(response.getEntity().getContent());
我發現了一些建議,這個應該怎麼使用HttpURLConnection
來完成,但是他們都複雜得多,那麼當前的解決方案(不能再次使用)。我正在談論執行與上述相同功能的許多代碼行。
有沒有人有一個良好的堅實較短的解決方案?
請參閱https://github.com/square/okhttp/wiki/Recipes#posting-a-file和https://github.com/square/okhttp/wiki/Recipes#posting-a-multipart-request for OkHttp食譜的潛在相關性。 – CommonsWare
@CommonsWare,感謝您的鏈接。我一直在閱讀OkHttp,並決定在我的應用中使用它。它爲我處理很多問題,使用起來非常簡單。謝謝你的提示。寫這個作爲答案,我會標記你。 – Ambran
另一種選擇,你可以參考,如果你想使用Volley http://stackoverflow.com/questions/32240177/working-post-multipart-request-with-volley-and-without-httpentity – BNK