天天看点

android 根据IP获取天气情况 详细讲解

分析:

   此功能必须在可用的网络下运行的,确保在有可用网络下才能正常运行,获取当前网络IP判断所在城市,通过城市查询天气

1、首先判断网络是否正常(笔者做的是平板应用的一个模块有手机有些功能不一样)

[java]  view plain copy

  1. public void getLocalIPAddress() {  
  2.         WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  3.         if (!wm.isWifiEnabled()) {  
  4.             Toast.makeText(this, "没有可用的网络", Toast.LENGTH_LONG).show();  
  5.         }  
  6.     }  

2、其次要自动获取IP地址(webservice借口、相关网址),在此,笔者是根据网址获取,在进行解析

getCityIP()

[java]  view plain copy

  1. public void getCityIP() {  
  2.         URL url;  
  3.         URLConnection conn = null;  
  4.         InputStream is = null;  
  5.         InputStreamReader isr = null;  
  6.         BufferedReader br = null;  
  7.         String str = "";  
  8.         org.jsoup.nodes.Document doc;  
  9.         try {  
  10.             url = new URL("http://city.ip138.com/city.asp");  
  11.             conn = url.openConnection();  
  12.             is = conn.getInputStream();  
  13.             isr = new InputStreamReader(is);  
  14.             br = new BufferedReader(isr);  
  15.             String input = "";  
  16.             while ((input = br.readLine()) != null) {  
  17.                 str += input;  
  18.             }  
  19.             doc = Jsoup.parse(str);  
  20.             String ip1 = doc.body().text();  
  21.             int start = ip1.indexOf("[");  
  22.             int end = ip1.indexOf("]");  
  23.             setIp(ip1.substring(start + 1, end));  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  

3、再次根据IP地址获取城市(webservice借口、解析网页)

getCityByIp():

[java]  view plain copy

  1. public void getCityByIp() {  
  2.             try {  
  3.                 URL url = new URL("http://whois.pconline.com.cn/ip.jsp?ip=" + getIp());  
  4.                 HttpURLConnection connect = (HttpURLConnection) url  
  5.                         .openConnection();  
  6.                 InputStream is = connect.getInputStream();  
  7.                 ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  8.                 byte[] buff = new byte[256];  
  9.                 int rc = 0;  
  10.                 while ((rc = is.read(buff, 0, 256)) > 0) {  
  11.                     outStream.write(buff, 0, rc);  
  12.                 }  
  13.                 System.out.println(outStream);  
  14.                 byte[] b = outStream.toByteArray();  
  15.                 // 关闭  
  16.                 outStream.close();  
  17.                 is.close();  
  18.                 connect.disconnect();  
  19.                 String address = new String(b,"GBK");  
  20.                 if (address.startsWith("北")||address.startsWith("上")||address.startsWith("重")){  
  21.                        setCity(address.substring(0,address.indexOf("市")));  
  22.                   }  
  23.                   if(address.startsWith("香")){  
  24.                      setCity(address.substring(0,address.indexOf("港")));  
  25.                   }  
  26.                   if(address.startsWith("澳")){  
  27.                    setCity(address.substring(0,address.indexOf("门")));  
  28.                     }  
  29.                     if (address.indexOf("省") != -1) {  
  30.                    setCity(address.substring(address.indexOf("省") + 1, address.indexOf("市")));  
  31.                     }  
  32.             } catch (Exception e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  

4、进行天气查询实现webservice接口(http://www.webxml.com.cn/webservices/weatherwebservice.asmx)

[java]  view plain copy

  1. private static final String NAMESPACE = "http://WebXml.com.cn/";  
  2.     // WebService地址  
  3.     private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";  
  4.     private static final String METHOD_NAME = "getWeatherbyCityName";  
  5.     private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";  
  6.     private String weatherToday;  
  7.     private SoapObject detail;  
  8.     private String weatherNow;  
  9.     private String weatherWillBe;  
  10.     private void setIcon(String weather, ImageView imageview) {  
  11.         if (weather.equalsIgnoreCase("nothing.gif"))  
  12.             imageview.setBackgroundResource(R.drawable.a_nothing);  
  13.         if (weather.equalsIgnoreCase("0.gif"))  
  14.             imageview.setBackgroundResource(R.drawable.a_0);  
  15.         if (weather.equalsIgnoreCase("1.gif"))  
  16.             imageview.setBackgroundResource(R.drawable.a_1);  
  17.         if (weather.equalsIgnoreCase("2.gif"))  
  18.             imageview.setBackgroundResource(R.drawable.a_2);  
  19.         if (weather.equalsIgnoreCase("3.gif"))  
  20.             imageview.setBackgroundResource(R.drawable.a_3);  
  21.         if (weather.equalsIgnoreCase("4.gif"))  
  22.             imageview.setBackgroundResource(R.drawable.a_4);  
  23.         if (weather.equalsIgnoreCase("5.gif"))  
  24.             imageview.setBackgroundResource(R.drawable.a_5);  
  25.         if (weather.equalsIgnoreCase("6.gif"))  
  26.             imageview.setBackgroundResource(R.drawable.a_6);  
  27.         if (weather.equalsIgnoreCase("7.gif"))  
  28.             imageview.setBackgroundResource(R.drawable.a_7);  
  29.         if (weather.equalsIgnoreCase("8.gif"))  
  30.             imageview.setBackgroundResource(R.drawable.a_8);  
  31.         if (weather.equalsIgnoreCase("9.gif"))  
  32.             imageview.setBackgroundResource(R.drawable.a_9);  
  33.         if (weather.equalsIgnoreCase("10.gif"))  
  34.             imageview.setBackgroundResource(R.drawable.a_10);  
  35.         if (weather.equalsIgnoreCase("11.gif"))  
  36.             imageview.setBackgroundResource(R.drawable.a_11);  
  37.         if (weather.equalsIgnoreCase("12.gif"))  
  38.             imageview.setBackgroundResource(R.drawable.a_12);  
  39.         if (weather.equalsIgnoreCase("13.gif"))  
  40.             imageview.setBackgroundResource(R.drawable.a_13);  
  41.         if (weather.equalsIgnoreCase("14.gif"))  
  42.             imageview.setBackgroundResource(R.drawable.a_14);  
  43.         if (weather.equalsIgnoreCase("15.gif"))  
  44.             imageview.setBackgroundResource(R.drawable.a_15);  
  45.         if (weather.equalsIgnoreCase("16.gif"))  
  46.             imageview.setBackgroundResource(R.drawable.a_16);  
  47.         if (weather.equalsIgnoreCase("17.gif"))  
  48.             imageview.setBackgroundResource(R.drawable.a_17);  
  49.         if (weather.equalsIgnoreCase("18.gif"))  
  50.             imageview.setBackgroundResource(R.drawable.a_18);  
  51.         if (weather.equalsIgnoreCase("19.gif"))  
  52.             imageview.setBackgroundResource(R.drawable.a_19);  
  53.         if (weather.equalsIgnoreCase("20.gif"))  
  54.             imageview.setBackgroundResource(R.drawable.a_20);  
  55.         if (weather.equalsIgnoreCase("21.gif"))  
  56.             imageview.setBackgroundResource(R.drawable.a_21);  
  57.         if (weather.equalsIgnoreCase("22.gif"))  
  58.             imageview.setBackgroundResource(R.drawable.a_22);  
  59.         if (weather.equalsIgnoreCase("23.gif"))  
  60.             imageview.setBackgroundResource(R.drawable.a_23);  
  61.         if (weather.equalsIgnoreCase("24.gif"))  
  62.             imageview.setBackgroundResource(R.drawable.a_24);  
  63.         if (weather.equalsIgnoreCase("25.gif"))  
  64.             imageview.setBackgroundResource(R.drawable.a_25);  
  65.         if (weather.equalsIgnoreCase("26.gif"))  
  66.             imageview.setBackgroundResource(R.drawable.a_26);  
  67.         if (weather.equalsIgnoreCase("27.gif"))  
  68.             imageview.setBackgroundResource(R.drawable.a_27);  
  69.         if (weather.equalsIgnoreCase("28.gif"))  
  70.             imageview.setBackgroundResource(R.drawable.a_28);  
  71.         if (weather.equalsIgnoreCase("29.gif"))  
  72.             imageview.setBackgroundResource(R.drawable.a_29);  
  73.         if (weather.equalsIgnoreCase("30.gif"))  
  74.             imageview.setBackgroundResource(R.drawable.a_30);  
  75.         if (weather.equalsIgnoreCase("31.gif"))  
  76.             imageview.setBackgroundResource(R.drawable.a_31);  
  77.     }  
  78.     public void getWeather(String cityName) {  
  79.         try {  
  80.             SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
  81.             rpc.addProperty("theCityName", cityName);  
  82.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(  
  83.                     SoapEnvelope.VER11);  
  84.             envelope.bodyOut = rpc;  
  85.             envelope.dotNet = true;  
  86.             envelope.setOutputSoapObject(rpc);  
  87.             HttpTransportSE ht = new HttpTransportSE(URL);  
  88.             ht.debug = true;  
  89.             ht.call(SOAP_ACTION, envelope);  
  90.             detail = (SoapObject) envelope.getResponse();  
  91.             parseWeather(detail);  
  92.         } catch (Exception e) {  
  93.             e.printStackTrace();  
  94.         }  
  95.     }  
  96.     private void parseWeather(SoapObject detail)  
  97.             throws UnsupportedEncodingException {  
  98.         textview1 = (TextView) this.findViewById(R.id.TextView01);  
  99.         String date = detail.getProperty(6).toString();  
  100.         // 当天天气  
  101.         weatherToday = "\n天气:" + date.split(" ")[1];  
  102.         weatherToday = weatherToday + "\n气温:"  
  103.                 + detail.getProperty(5).toString();  
  104.         weatherToday = weatherToday + "\n风力:"  
  105.                 + detail.getProperty(7).toString() + "\n";  
  106.         weatherNow = detail.getProperty(8).toString();  
  107.         weatherWillBe = detail.getProperty(9).toString();  
  108.         textview1.setText(getIp() + '\n' + getCity() + "\n今天"  
  109.                 + weatherToday);  
  110.         setIcon(weatherNow, image1);  
  111.         setIcon(weatherWillBe, image2);  
  112.     }  

5、最后在AndroidMainifest.xml加入权限

[java]  view plain copy

  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>   
  4. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>   
  5. <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>   

 本文转载来自liushengmenghttp://blog.csdn.net/liushengmeng/article/details/8202425