在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+中我也找不到推荐的方法来进行POST请求。那么现在应该如何进行POST请求呢?
HttpUrlConnection
وˆ–و¥è‡ھSquareçڑ„okhttpم€‚ - Raghunandan