Android 网络请求
##HttpClient (Apache)
6.0中已经移除了这个方法,Google建议使用HttpUrlConnection 见Android 6.0 Changes
###GET 方法
新建一个HttpGet对象
获取HtttpClient实例
获取HttpResponse实例 并传入HttpGet对象
判断是否请求成功
POST 方法
新建一个HttpGet对象
获取HtttpClient实例
设置POST参数(1)
设置POST参数(2)
1
2
3
4
5
6
7NameValuePair namePair = new BasicNameValuePair("username","username");
NameValuePair pswPair = new BasicNameValuePair("password","password");
List<NameValuePair> pairList = new ArrayList<NameValuePair>();
pariList.add(namePair);
//添加内容到请求中
HttpEntity httpEntity = new UrlEncodedFormEntity(pairlist);
httpPost.setEntity(httpEntity);获取HttpResponse实例 并传入HttpGet对象
判断是否请求成功
1
2
3
4
5
6if(response.getStatusLine().getStatusCode()==200)
{//do something
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
//String result = EntityUtils.toString(response.getEntity(),”UTF-8”);//带中文的话
}
Gitalk 加载中 ...