Android网络常用的请求---HttpURLConnection和HttpClient
来源:互联网 发布:byte数组oracle数据库 编辑:程序博客网 时间:2024/06/05 04:12
<span style="font-size:18px;">/** * method one: * */ public static void sendHttpRequest(final String urlStr, final HttpCallBackListener listenser) { new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } if (listenser != null) { listenser.onfinish(response.toString()); } } catch (Exception e) { if (listenser != null) { listenser.onError(e); } } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } public interface HttpCallBackListener { void onfinish(String response); void onError(Exception e); } /** * method two: HttpClient--HttpGet * */ public String cookieStr = ""; public String sendRequestWithHttpClient(final String urlStr) { String response = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(urlStr); httpGet.setHeader("Cookie", cookieStr); HttpResponse httpResponse; try { httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity entity = httpResponse.getEntity(); response = EntityUtils.toString(entity, "utf-8"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; } /** * method two: HttpClient--HttpPost * requestBody:"<Result><UserName>MeiMei</UserName></Result>" */ public synchronized String post(String requestBody, URI url) { String requestResult = null; HttpPost httpRequest = new HttpPost(); httpRequest.setURI(url); httpRequest.setHeader("Cookie", cookieStr); try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("inputXml", requestBody)); httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Header[] headers = httpResponse.getAllHeaders(); for (Header header : headers) { if ("Set-Cookie".equals(header.getName())) { String temp = header.getValue(); int index = temp.indexOf("Version="); cookieStr += temp.substring(0, index); } } requestResult = EntityUtils.toString(httpResponse.getEntity()); } } catch (Exception e) { e.printStackTrace(); } return requestResult; }</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;">以上是平时常用的http请求,在这里做了一下小结,平时项目中可以根据需要来选择使用。</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>
0 0
- Android网络常用的请求---HttpURLConnection和HttpClient
- 常用的几种网络请求OkHttpClient , HttpClient和 HttpURLConnection
- Android 网络请求HttpURLConnection 和 HttpClient详解
- Android应用开发网络请求 HttpClient和HttpURLConnection的区别
- Android网络请求库对比之HttpUrlConnection和HttpClient
- Android网络请求操作httpurlconnection和httpclient基本使用
- Android网络HttpURLConnection和HttpClient
- Android网络HttpURLConnection和HttpClient
- Android中网络请求—HttpClient、HttpURLConnection
- HttpURLConnection、HttpClient网络请求
- 网络请求部分-------HttpClient和HttpURLConnection
- HttpClient和HttpURLConnection网络请求数据
- HttpClient和HttpURLConnection网络请求数据
- 网络请求HttpURLConnection与HttpClient的使用
- android 联网请求的两种方式HttpURLConnection和HttpClient
- HttpClient和HttpURLConnection请求
- 【代码】Android的网络通信详解HttpUrlConnection和HttpClient
- HttpClient和HttpUrlConnection的请求网路
- json解析 回车符数据过滤的正确姿势
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).错误解决方法
- 自定义控件其实很简单1/2
- 日期的校验和设置
- ANT+BAT 打包Android程序
- Android网络常用的请求---HttpURLConnection和HttpClient
- java多线程之CountDownLatch类基本用法
- Android签名详解
- 10个经典的Android开源应用项目
- BeanUtils工具的学习与介绍
- Android关机流程源码分析
- 如何制作自适应的网页
- 禁用物理键盘menu,将 app:showAsAction="never"属性的menu在ActionBar显示出来
- IOS Monkey测试UI AutoMonkey