Android(安卓)HttpClient
16lz
2021-01-24
使用Apache提供的HttpClient接口进行HTTP操作。
GET方法:
//http地址StringhttpUrl=;//HttpGet连接对象HttpGethttpRequest=newHttpGet(httpUrl);//取得HttpClient对象HttpClienthttpclient=newDefaultHttpClient();//请求HttpClient,取得HttpResponseHttpResponsehttpResponse=httpclient.execute(httpRequest);//请求成功if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//取得返回的字符串StringstrResult=EntityUtils.toString(httpResponse.getEntity());System.out.println(strResult);}else{System.out.println("请求错误!");}
POST方法:
//http地址StringhttpUrl="http://192.168.1.110:8080/httpget.jsp";//HttpPost连接对象HttpPosthttpRequest=newHttpPost(httpUrl);//使用NameValuePair来保存要传递的Post参数List<NameValuePair>params=newArrayList<NameValuePair>();//添加要传递的参数params.add(newBasicNameValuePair("par","HttpClient_android_Post"));//设置字符集HttpEntityhttpentity=newUrlEncodedFormEntity(params,"gb2312");//请求httpRequesthttpRequest.setEntity(httpentity);//取得默认的HttpClientHttpClienthttpclient=newDefaultHttpClient();//取得HttpResponseHttpResponsehttpResponse=httpclient.execute(httpRequest);//HttpStatus.SC_OK表示连接成功if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){//取得返回的字符串StringstrResult=EntityUtils.toString(httpResponse.getEntity());System.out.println(strResult);}else{System.out.println("请求错误!");}
更多相关文章
- android警告 —— This Handler class should be static
- android最近老出现这样的问题: ADB server didn't ACK
- Android(安卓)pull解析
- android自动工具类TextUtils使用详解
- Android微信SDK实现分享
- Android二进制文件转码到Base64并通过Post进行提交
- android实现PC到手机的SOCKET通信
- 打开Android(安卓)Studio报错 "required plugin "Android(安卓)S