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、异步请求

 1 /** 2  * 异步请求  3  * @author admin  4      */ 5     class GetData extends AsyncTask< String , Integer , String >{  6  7  @Override  8         protected String doInBackground(String... params) {  9             HttpUtil httpUtil = new HttpUtil() ; 10             String resutl = httpUtil.httpGet( params[0] ) ; 11             if( resutl == null ){ 12                 return "" ; 13  } 14             return resutl ; 15  } 16 17  @Override 18         protected void onPostExecute(String result) { 19             super.onPostExecute(result); 20  } 21     }

Android Http请求框架二:xUtils 框架网络请求

更多相关文章

  1. Android中子线程网络查看器与Handler消息处理器
  2. Android向Http服务器发送Http请求异常-UnknowHostException
  3. Android字体设置
  4. Android保持屏幕常亮的方法总结
  5. Android如何使用selector设置RelativeLayout的背景
  6. Android(安卓)Glide设置图片圆角,亲测有效
  7. Android(安卓)ViewGroup系列控件的使用
  8. Android动态设置View的位置和大小
  9. kotlin 开发 android 程序中网络http请求和线程的使用

随机推荐

  1. Android内存优化
  2. Android群英传笔记——第一章:Android体系
  3. Android工程目录结构 Android四大组件 Co
  4. Android(安卓)日历开发教程[一]
  5. android每日一问1【2011-8-30】
  6. Android(安卓)Service生命周期及用法
  7. View的使用方法
  8. Android实战经验之图像处理及特效处理的
  9. android的Activity窗口模式
  10. Android(安卓)API Guides---Creating an