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

来源:互联网 发布:泽国 unity3d 编辑:程序博客网 时间:2024/04/30 13:54

分析:

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

 

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

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


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

getCityIP()

[java] view plaincopy
  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.   
  28.     }  


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

getCityByIp():

[java] view plaincopy
  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, 0256)) > 0) {  
  11.                     outStream.write(buff, 0, rc);  
  12.                       
  13.                 }  
  14.                 System.out.println(outStream);  
  15.                 byte[] b = outStream.toByteArray();  
  16.                   
  17.                 // 关闭  
  18.                 outStream.close();  
  19.                 is.close();  
  20.                 connect.disconnect();  
  21.                 String address = new String(b,"GBK");  
  22.                 if (address.startsWith("北")||address.startsWith("上")||address.startsWith("重")){  
  23.                        setCity(address.substring(0,address.indexOf("市")));  
  24.                   }  
  25.                   if(address.startsWith("香")){  
  26.                      setCity(address.substring(0,address.indexOf("港")));  
  27.                   }  
  28.                   if(address.startsWith("澳")){  
  29.                    setCity(address.substring(0,address.indexOf("门")));  
  30.                     }  
  31.                     if (address.indexOf("省") != -1) {  
  32.                    setCity(address.substring(address.indexOf("省") + 1, address.indexOf("市")));  
  33.                     }  
  34.             } catch (Exception e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  

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

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


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

[java] view plaincopy
  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>   

原创粉丝点击