当前位置: 代码迷 >> Android >> java.net.UnknownHostException: Host is unresolved错误
  详细解决方案

java.net.UnknownHostException: Host is unresolved错误

热度:80   发布时间:2016-05-01 21:59:45.0
java.net.UnknownHostException: Host is unresolved异常
Java code
public void createXmlResource(String city, OutputStream out) {                BufferedReader br;                try {                        URL url = new URL("http://www.google.com/ig/api?weather=北京");                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();                        conn.setDoInput(true);                        conn.connect();                        br = new BufferedReader(new InputStreamReader(conn.getInputStream()));                        OutputStreamWriter file_writer = new OutputStreamWriter(out);                        String str = null;                        while ((str = br.readLine()) != null) {                                file_writer.write(str);                        }                        br.close();                        file_writer.close();                } catch (Exception e) {                        e.printStackTrace();                }        }

结果conn.connect();这一句 抛出了 
java.net.UnknownHostException: Host is unresolved: www.google.com:80 
在AndroidManifest.xml中我也写了<uses-permission android:name="android.permission.INTERNET" /> 
希望知道的同学告诉我真相啊

------解决方案--------------------
还是网络问题,你模拟器还是真机,先用下系统的浏览器,看能打开google不,然后测试你的程序。感觉网络不通
------解决方案--------------------
楼主把URL地址改为:http://www.google.com.hk/ig/api?weather=北京
试试,因为现在谷歌在中国的域名是www.google.com.hk
------解决方案--------------------
不能用中文,必须用城市的英文名称(即拼音)
http://www.google.com/ig/api?weather=beijing
http://www.google.com.hk/ig/api?weather=beijing
两个都行。

要想在模拟器上或真机上显示简体中文,需要用
http://www.google.com/ig/api?hl=zh-cn&weather=Beijing
并把解析得到的数据重新设置utf-8编码
  相关解决方案