Android Http请求框架一:Get 和 Post 请求
来源:互联网 发布:精灵恢复软件 注册码 编辑:程序博客网 时间:2024/05/18 11:47
1、HttpUtil
package com.app.android01;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.CoreConnectionPNames;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;public class HttpUtil { /** * get请求 * @param httpUrl * @return * @throws */ public String httpGet( String httpUrl ){ String result = "" ; try { BufferedReader reader = null; StringBuffer sbf = new StringBuffer() ; URL url = new URL( httpUrl ) ; HttpURLConnection connection = (HttpURLConnection) url.openConnection() ; //设置超时时间 10s connection.setConnectTimeout(10000); //设置请求方式 connection.setRequestMethod( "GET" ) ; connection.connect(); InputStream is = connection.getInputStream() ; reader = new BufferedReader(new InputStreamReader( is , "UTF-8" )) ; String strRead = null ; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * httpPost请求 * @param httpUrl * @return */ public String httpPost( String httpUrl ){ String result = "" ; // 第一步,创建HttpPost对象 HttpPost httpPost = new HttpPost( httpUrl ); // 设置HTTP POST请求参数必须用NameValuePair对象 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("action", "downloadAndroidApp")); params.add(new BasicNameValuePair("packageId", "89dcb664-50a7-4bf2-aeed-49c08af6a58a")); params.add(new BasicNameValuePair("uuid", "test_ok1")); HttpResponse httpResponse = null; try { // 设置httpPost请求参数 httpPost.setEntity(new UrlEncodedFormEntity( params , HTTP.UTF_8 )); HttpClient httpClient = new DefaultHttpClient() ; // 请求超时 10s httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000 ) ; // 读取超时 10s httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000 ); httpResponse = httpClient.execute( httpPost ) ; if (httpResponse.getStatusLine().getStatusCode() == 200) { // 第三步,使用getEntity方法活得返回结果 result = EntityUtils.toString(httpResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result ; } }
2、异步请求
/** * 异步请求 * @author admin */ class GetData extends AsyncTask< String , Integer , String >{ @Override protected String doInBackground(String... params) { HttpUtil httpUtil = new HttpUtil() ; String resutl = httpUtil.httpGet( params[0] ) ; if( resutl == null ){ return "" ; } return resutl ; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } }
0 0
- Android Http请求框架一:Get 和 Post 请求
- Android中HTTP Post和Get请求
- android的http请求get和post请求方式
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- Android Http get post请求
- HTTP get和post请求
- http post和get请求
- Http协议(get请求和post请求)
- HTTP post请求和get请求
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- 机械硬盘HDD
- 基于V4L2驱动程序的USB摄像头Android(JNI)的编写(一)
- 《父与子的编程之旅》——可计数与不可计数的循环
- MySQL文件结构
- CUDA安装
- Android Http请求框架一:Get 和 Post 请求
- bzoj1188: [HNOI2007]分裂游戏
- html5 canvas画图
- poj 3984 迷宫问题(BFS+回溯)
- 文件的存取
- UML图
- HDU 4229 Vive la Difference!
- Android Http请求框架二:xUtils 框架网络请求
- STL中 map的用法