我的Andoird應用程序需要註冊設備的令牌才能推送通知。到現在爲止,我一直用這個代碼:Java/Android:如何更新已棄用的org.apache.http
final HttpClient httpclient = new DefaultHttpClient();
final HttpPost httppost = new HttpPost("http://mysite/registration.php");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.e("token:", token);
Log.e("android_id:", android_id);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("token", token));
nameValuePairs.add(new BasicNameValuePair("device", android_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); /***/
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost); /***/
String responseBody = EntityUtils.toString(response.getEntity()); /***/
不管怎樣,Android的工作室不斷講這個代碼已被棄用,我希望我不是被迫加入Apache的依賴性。例如,我不想在我的build.gradle中有
dependencies {
compile group: 'org.apache.httpcomponents' ,
name: 'httpclient-android' ,
version: '4.3.5.1'
}
。
那麼,如何更新我的代碼?特別是,我在理解如何使用/ *** /更新行時遇到困難。 感謝您的關注
將這個'useLibrary「org.apache.http.legacy''在'的Android {...}'部分或'build.gradle'文件省心省力;) – Shark
@Shark正如我在我的問題中所說的,我不想使用這樣的棄用類。沒有更好更新的解決方案嗎? –
https://stackoverflow.com/questions/32949626/android-m-org-apache-http-entity-fileentity-deprecated – CommonsWare