当前位置:编程学习 > wap >>

哪位高手帮忙指导下,java.net.UnknownHostException: www.weather.com.cn

想自己写个天气软件练练手,中间发生了状况了,代码如下
private String connServerForResult(String strUrl) 
{
//获取HttpGet对象
HttpGet httpRequest = new    HttpGet("http://m.weather.com.cn/data/101010200.html");
String strResult = "";
try 
{
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
{
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) 
{
System.out.println("weather log str =======================error1");
e.printStackTrace();
} catch (IOException e) 
{
System.out.println("weather log str =======================error2");
e.printStackTrace();
}
Log.i("Infor", strResult);
return strResult; //返回结果
}

在模拟器上运行的,手机也试过,报:java.net.UnknownHostException: www.weather.com.cn这个错误访问不到www.weather.com.cn这个网站呢 --------------------编程问答-------------------- 1、你的电脑或模拟器没有网络,请检查,也有可能是是网络地址错误,网站倒闭或者后台地址失效。

2、java.net.UnknownHostException: Host is unresolved ***.****.*** 80

你若是确定你的网络权限在配置文件中写好,也确定自己的网络是好的,

3、java.net.UnknownHostException:www.***.com

无法解析域名的错误,可以将域名对应的ip写入到hosts文件中保存。

windows的hosts文件在“c:\windows\system32\drivers\etc”中。

Linux更改hosts的操作如下:


cd /etc/
vi hosts
输入:
211.151.71.11 www.xxxx.com
4、还有一种问题是手机网络设置的问题,遇到这种问题可以更换下手机网络设置改为3gnet或者其他的网络。
下面是一个检测网络类型的程序,可供大家来检测手机网络类型(也可以在手机中设置中直接查看):
[java] view plaincopy
public String NetType(Context context) {  
        try {  
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
            NetworkInfo info = cm.getActiveNetworkInfo();  
            String typeName = info.getTypeName().toLowerCase(); // WIFI/MOBILE  
            if (typeName.equalsIgnoreCase("wifi")) {  
            } else {  
                typeName = info.getExtraInfo().toLowerCase();  
                // 3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap  
            }  
            return typeName;  
        } catch (Exception e) {  
            return null;  
        }  
    }   --------------------编程问答-------------------- 谢谢楼上的回复,这个我试过了,以上试过啦,还是没解决问题 --------------------编程问答-------------------- 你的模拟器能不能联网?
把LOG也贴出来看看咯 --------------------编程问答-------------------- 权限问题。。 --------------------编程问答-------------------- 最后一楼正解!老把这茬子给忘记了。没记性啊。 --------------------编程问答-------------------- 你的网络是不是设置了代理环境,如果是的话,你将不能直接按照正常的网络环境来访问外网,需要针对URL进行设置。 --------------------编程问答-------------------- 大爷解决了吗????? --------------------编程问答-------------------- 权限配了吗?
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,