【发布时间】:2015-06-14 16:26:36
【问题描述】:
org.apache.http 类和 AndroidHttpClient 类已在 Android 5.1 中弃用。这些类不再维护,您应该尽快将使用这些 API 的任何应用代码迁移到 URLConnection 类。
https://developer.android.com/about/versions/android-5.1.html#http
建议切换到 URLConnection 类。没有足够的文档确切地说明如何从应用程序发出后调用。
目前我正在使用这个
public void postData()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
}
我如何使用 UrlConnections 来做到这一点?
【问题讨论】:
-
你的意思是这样的 - stackoverflow.com/questions/9767952/… 吗?我建议您实际上切换到 Retrofit、aQuery 或任何其他 HTTP 库。
-
@stan NameValuePair 类已弃用,该类在您的共享链接接受的答案中使用