作者也一直遇到这个问题,很奇怪,用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里面的数据,好像移动和联通都是一样的。都是这个代理地址。
分享到:
相关推荐
Httpclient上传文件,非常有用的jar包 apache-mime4j-0.6.jar httpmime-4.0.jar
Android HttpClient基本使用方法 测试过能用。
struts2 android httpclient 上传文件 客服端 服务器端 亲测通过
这是一个用Httpclient设置代理下载文件的例子
利用 android httpclient 编写http协议的例子代码,可以参考添加post,get ,put delete 等功能。工程师文件可以直接编译运行。
android9.0不再支持useLibrary 'org.apache.http.legacy'引入httpclient,需要引入jar
该Demo主要介绍使用HttpClient获取网络数据,然后经过JSON后显示出来。另外本文章用到了常用的异步加载技术,AsyncTask类。
android的Http上载文件 可以实现多文件上载,结合android自身白日提供的强大的HttpClient操作。
android应用开发 httpClient所需jar包 和调用webservice 工具ksoap2 jar 包
Android利用HttpClient实现访问web服务器源码 剥离出HttpClient代码,用来做网页爬虫亦可
android HttpClient 获取网络图片 实例
下面给大家展示了AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言: 这类其实是Google对阿帕奇的HttpClient的一个封装,一些默认属性有android做了一些...
是android的jar中httpclient的源码,是org.apache.http包的源码。其中包含org.apache.http.HttpResponse类。 不是官网上下载的,官网上的源码里面没有org.apache.http.HttpResponse这个类。
Android-异步HttpClient框架,采用两种提交方式 Android-异步HttpClient框架(get和post) Android-异步HttpClient框架 csdn博客地址:http://blog.csdn.net/u014657752
Android 使用HttpClient代理
android HttpClient访问某些Https时,出现了问题,无法访问,好像是要安全验证。此Demo解决了此问题,HttpClient能够Https和Http类型的URL了。 在eclipse下打开工程若有乱码,请把eclipse的字符编码改成UTF-8。
Android Asynchronous HTTPClient的实现和优化
基于Android HttpClient的一个网络连接简单实现,附jar包及源码。
Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...
Android使用HttpClient上传文件到服务器完整实例,包括要用到的jar都下libs下面,服务器的源码也有