Android SDK中包含了HttpClient,在Android6.0版本直接删除了HttpClient类库,如果仍想使用则解决方法是:
- 如果使用的是eclipse则在libs中加入org.apache.http.legacy.jar
这个jar包在:**sdk\platforms\android-23\optional目录中(需要下载android
6.0的SDK) - 如果使用的是android studio则 在相应的module下的build.gradle中加入:
android {
useLibrary 'org.apache.http.legacy'
}
GET:
首先我们来用DefaultHttpClient类来实例化一个HttpClient,并配置好默认的请求参数:
public HttpClient creatHttpClient(){
//更多请求设置http://www.mamicode.com/info-detail-52214.html
//新建Myparams为基本参数
HttpParams Myparams=new BasicHttpParams();
//设置链接超时参数
HttpConnectionParams.setConnectionTimeout(Myparams,8000);
//设置响应超时参数
HttpConnectionParams.setSoTimeout(Myparams,5000);
//设置是否延迟发送
HttpConnectionParams.setTcpNoDelay(Myparams,true);
//设置请求Http协议版本
HttpProtocolParams.setVersion(Myparams, HttpVersion.HTTP_1_1);
//设置请求Http内容编码方式
HttpProtocolParams.setContentCharset(Myparams, HTTP.UTF_8);
//设置连续握手
HttpProtocolParams.setUseExpectContinue(Myparams,true);
HttpClient myHttpClient=new DefaultHttpClient(Myparams);
return myHttpClient;
}
接下来创建HttpGet和HttpClient,请求网络并得到HttpResponse,并对HttpResponse进行处理:
public void GetHttpClient(String url){
//确定为GET方式请求
HttpGet httpGet=new HttpGet(url);
httpGet.addHeader("Connection","Keep-Alive");
try {
HttpClient client=creatHttpClient();
HttpResponse httpResponse=client.execute(httpGet);
//状态码
int code=httpResponse.getStatusLine().getStatusCode();
//实体
HttpEntity entity=httpResponse.getEntity();
Log.d(TAG, "GetHttpClient:状态码 "+code);
if (code==200&&entity!=null){
InputStream in=entity.getContent();
//字节流转成字符串(字符流)
String Content=converStreamToString(in);
Log.d(TAG, "GetHttpClient: 内容"+Content);
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
converStreamToString方法将请求结果转换成String类型:
private String converStreamToString(InputStream in) {
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder builder=new StringBuilder();
String line=null;
try {
while((line=reader.readLine())!=null){
builder.append(line+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
String Content=builder.toString();
return Content;
}
最后我们开启线程访问
new Thread(new Runnable() {
@Override
public void run() {
GetHttpClient("http://www.ccdio.cn/blog/validate.do");
}
}).start();
POST方式:
private void useHttpClientPost(String url) {
HttpPost mHttpPost = new HttpPost(url);
mHttpPost.addHeader("Connection", "Keep-Alive");
try {
HttpClient mHttpClient = createHttpClient();
List<NameValuePair> postParams = new ArrayList<>();
//要传递的参数
postParams.add(new BasicNameValuePair("username", "moon"));
postParams.add(new BasicNameValuePair("password", "123"));
mHttpPost.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
int code = mHttpResponse.getStatusLine().getStatusCode();
if (null != mHttpEntity) {
InputStream mInputStream = mHttpEntity.getContent();
String respose = converStreamToString(mInputStream);
Log.i("wangshu", "请求状态码:" + code + "\n请求结果:\n" + respose);
mInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
文章来自:http://blog.csdn.net/itachi85/article/details/51010109