android httpclient Host is unresolved 错误解决方案

作者也一直遇到这个问题,很奇怪,用wifi连接,没有问题,关闭wifi或者不在wifi区域,就无法连接我的服务器。

读者可先看这篇文章 http://blog.csdn.net/dropWater_yjqbll/archive/2010/12/16/6079531.aspx

无法连接的主要原因是我们的手机都是通过移动或者联通代理出去的请求,所以无法直接发送出去。

自己写了类如下:

public static String request(int netType, String host, String url,  
            int method, List<NameValuePair> pamrams) {  
        if(netType==HttpUtil.WAP_INT){ //wap上网  
            HttpRequestBase request = null;  
            String strReust = null;  
            try { //wap  
                //截取 http://klmu.v228.10000net.cn/publicbicycle 为 klmu.v228.10000net.cn  
                HttpHost target = new HttpHost(getHostStr(host),Integer.parseInt(getPort(host)));  
                if (method == 0) {  
                    request = new HttpPost(getUrl(host,url));  
                    if (pamrams != null) {  
                        ((HttpPost) request).setEntity(new UrlEncodedFormEntity(  
                                pamrams, HTTP.UTF_8));  
                    }  
                } else if (method == 1) {  
                    request = new HttpGet(url);  
                }  
                //新建HttpClient对象   
                DefaultHttpClient httpClient = new DefaultHttpClient();  
                HttpHost proxy = new HttpHost("10.0.0.172", 80);  
                httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  
                        proxy);  
                HttpResponse httpResponse = httpClient.execute(target, request);  
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                    strReust = EntityUtils.toString(httpResponse.getEntity());  
                } else {  
                    strReust = HTTPERROR_Start + " 服务器httpCode: "+ httpResponse.getStatusLine().getStatusCode() + HTTPERROR_END;  
                }  
                httpClient.getConnectionManager().shutdown();  
            } catch (Exception e) {  
                strReust = HTTPERROR_Start + e.getMessage() + HTTPERROR_END;  
            }  
            return strReust;  
        } else if(netType==HttpUtil.WIFI_INT){ //wifi  
            url = host+url;  
            HttpRequestBase request = null;  
            String strReust = null;  
            try {  
                if (method == 0) {  
                    request = new HttpPost(url);  
                    if (pamrams != null) {  
                        ((HttpPost) request).setEntity(new UrlEncodedFormEntity(  
                                pamrams, HTTP.UTF_8));  
                    }  
                } else if (method == 1) {  
                    request = new HttpGet(url);  
                }  
                // 设置连接超时时间和数据读取超时时间   
//              HttpParams httpParams = new BasicHttpParams();  
//              HttpConnectionParams.setConnectionTimeout(httpParams, 500);  
//              HttpConnectionParams.setSoTimeout(httpParams, 60 * 1000);  
                //新建HttpClient对象   
                //HttpClient httpClient = new DefaultHttpClient(httpParams);  
                HttpClient httpClient = new DefaultHttpClient();  
                HttpResponse httpResponse = httpClient.execute(request);  
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                    strReust = EntityUtils.toString(httpResponse.getEntity());  
                } else {  
                    strReust = HTTPERROR_Start + " 服务器httpCode: "+ httpResponse.getStatusLine().getStatusCode()+ HTTPERROR_END;  
                }  
                httpClient.getConnectionManager().shutdown();  
            } catch (Exception e) {  
                strReust = HTTPERROR_Start + e.getMessage() + HTTPERROR_END;  
            }  
            return strReust;  
        } else {  
            return  HTTPERROR_Start + "无法连接网络!" + HTTPERROR_END;  
        }  
    }
 * <Description>
* 1:wifi  
* 2:wap  
* 3:无法取得网络  
* @since May 10, 2011  
* @param conn  
* @return <Description>  
*  
*/  
ublic static int getNetType(Context ctx) {  
ConnectivityManager conn = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);  
if (conn == null){  
    return HttpUtil.NONET_INT;  
}  
NetworkInfo info = conn.getActiveNetworkInfo();  
if (info == null){  
    return HttpUtil.NONET_INT;  
}  
String type = info.getTypeName();//MOBILE(GPRS);WIFI  
Log.v("tag", "NetworkType=" + type);  
if (type.equals("WIFI")) {  
    return HttpUtil.WIFI_INT;  
} else {//if (type.equals("MOBILE")) {  
    return HttpUtil.WAP_INT;  
}
 由于我是用移动的卡,所以HttpHost proxy = new HttpHost("10.0.0.172", 80); 这个写死了,这里应该取apn里面的数据,好像移动和联通都是一样的。都是这个代理地址。

相关推荐