在 Android API 22 之前,我只是做了以下事情:
/**
*
* @param url
* @param params
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public InputStream getStreamFromConnection(String url, List<NameValuePair> params) throws ClientProtocolException, IOException {
if(ConnectionDetector.isConnectedToInternet(this.context)) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return httpEntity.getContent();
} else {
return null;
}
}
上面代码中的几乎所有内容现在都已弃用(NameValuePair
、DefaultHttpClient
、HttpPost
、UrlEncodedFormEntity
、HttpResponse
、HttpEntity
、ClientProtocolException
),我无法在 API 22+ 中找到推荐的方法。我现在应该如何发帖?
最佳答案
您现在可以使用 HttpURLConnection,您可以在下面的链接中找到完整的说明:
http://developer.android.com/reference/java/net/HttpURLConnection.html
关于android - DefaultHttpClient、HttpPost 等已弃用。现在怎么发帖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657292/