使用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("请求错误!");}

更多相关文章

  1. android警告 —— This Handler class should be static
  2. android最近老出现这样的问题: ADB server didn't ACK
  3. Android(安卓)pull解析
  4. android自动工具类TextUtils使用详解
  5. Android微信SDK实现分享
  6. Android二进制文件转码到Base64并通过Post进行提交
  7. android实现PC到手机的SOCKET通信
  8. 打开Android(安卓)Studio报错 "required plugin "Android(安卓)S

随机推荐

  1. Android使用控件Spinner实现下拉菜单列表
  2. android判断网络状态
  3. Android全透明状态栏
  4. 对Android中Tab的使用总结
  5. Android(安卓)Adobe工具类
  6. android 语音识别
  7. Android出现java.net.SocketException: P
  8. Android配置ip地址
  9. Android中的一些方法-----生生不息
  10. java.lang.IllegalStateException: Actio