当前位置:操作系统 > 安卓/Android >>

android获取URLConnection和HttpClient网络请求响应码

前段时间,有朋友问我网络请求怎么监听超时,这个我当时也没有没有做过,就认为是try....catch...获取异常,结果发现没有获取到,今天有时间,研究了一下,发现是从响应中来获取的对象中获取的,下面我把自己写的URLConnection和HttpClient网络请求响应码的实体共享给大家,希望对大家有帮助!

    

[java]package com.zhangke.product.platform.http.json; 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.InetSocketAddress; 
import java.net.Proxy; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
 
import org.apache.http.Header; 
import org.apache.http.HttpHost; 
import org.apache.http.HttpResponse; 
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.conn.ClientConnectionRequest; 
import org.apache.http.conn.params.ConnRoutePNames; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
 
import com.zhangke.product.platform.util.NetworkUtil; 
 
import android.content.Context; 
import android.util.Log; 
/**
 * @author spring sky
 * QQ 840950105
 * Email :vipa1888@163.com
 * 版权:spring sky
 * This class use in for request server and get server respnonse data
 * 
 *
 */ 
public class NetWork { 
    /**
     *   网络请求响应码
     *   <br>
     */ 
    private int responseCode = 1; 
    /**
     *  408为网络超时
     */ 
    public static final int REQUEST_TIMEOUT_CODE = 408; 
     
    /**
     * 请求字符编码
     */ 
    private static final String CHARSET = "utf-8"; 
    /**
     * 请求服务器超时时间
     */ 
    private static final int REQUEST_TIME_OUT = 1000*10;  
    /**
     * 读取响应的数据时间
     */ 
    private static final int READ_TIME_OUT = 1000*5; 
    private Context context ; 
     
    public NetWork(Context context) { 
        super(); 
        this.context = context; 
    } 
    /**
     * inputstream to String type 
     * @param is
     * @return
     */ 
    public String getString(InputStream is ) 
    { 
        String str = null; 
        try { 
            if(is!=null) 
            { 
                BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET)); 
                String line = null; 
                StringBuffer sb = new StringBuffer(); 
                while((line=br.readLine())!=null) 
                { 
                    sb.append(line); 
                } 
                str = sb.toString(); 
                if(str.startsWith("<html>"))   //获取xml或者json数据,如果获取到的数据为xml,则为null  
                { 
                    str = null; 
                } 
            } 
             
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
    /**
     * httpClient request type 
     * @param requestURL
     * @param map
     * @return
     */ 
    public InputStream requestHTTPClient(String requestURL,Map<String, String> map) 
    { 
        InputStream inputStream = null; 
        /**
         * 添加超时时间
         */ 
        BasicHttpParams httpParams = new BasicHttpParams(); 
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIME_OUT); 
        HttpConnectionParams.setSoTimeout(httpParams, READ_TIME_OUT); 
        HttpClient httpClient = new DefaultHttpClient(httpParams); 
         
        if (NetworkUtil.getNetworkType() == NetworkUtil.WAP_CONNECTED) { 
            HttpHost proxy = new HttpHost("10.0.0.172", 80)

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,