Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 根據IP獲取天氣情況 詳細講解

android 根據IP獲取天氣情況 詳細講解

編輯:關於Android編程

分析:

   此功能必須在可用的網絡下運行的,確保在有可用網絡下才能正常運行,獲取當前網絡IP判斷所在城市,通過城市查詢天氣

 

1、首先判斷網絡是否正常(筆者做的是平板應用的一個模塊有手機有些功能不一樣)

[java] 
public void getLocalIPAddress() { 
 
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
        if (!wm.isWifiEnabled()) { 
            Toast.makeText(this, "沒有可用的網絡", Toast.LENGTH_LONG).show(); 
        } 
    } 

2、其次要自動獲取IP地址(webservice借口、相關網址),在此,筆者是根據網址獲取,在進行解析

getCityIP()

[java] 
public void getCityIP() { 
        URL url; 
        URLConnection conn = null; 
        InputStream is = null; 
        InputStreamReader isr = null; 
        BufferedReader br = null; 
        String str = ""; 
        org.jsoup.nodes.Document doc; 
        try { 
            url = new URL("asp">http://city.ip138.com/city.asp"); 
            conn = url.openConnection(); 
            is = conn.getInputStream(); 
            isr = new InputStreamReader(is); 
            br = new BufferedReader(isr); 
            String input = ""; 
            while ((input = br.readLine()) != null) { 
                str += input; 
            } 
            doc = Jsoup.parse(str); 
            String ip1 = doc.body().text(); 
            int start = ip1.indexOf("["); 
            int end = ip1.indexOf("]"); 
            setIp(ip1.substring(start + 1, end)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
 
    } 

3、再次根據IP地址獲取城市(webservice借口、解析網頁)

getCityByIp():

[java] 
public void getCityByIp() { 
            try { 
                URL url = new URL("jsp?ip">http://whois.pconline.com.cn/ip.jsp?ip=" + getIp()); 
                HttpURLConnection connect = (HttpURLConnection) url 
                        .openConnection(); 
                InputStream is = connect.getInputStream(); 
                ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
                byte[] buff = new byte[256]; 
                int rc = 0; 
                while ((rc = is.read(buff, 0, 256)) > 0) { 
                    outStream.write(buff, 0, rc); 
                     
                } 
                System.out.println(outStream); 
                byte[] b = outStream.toByteArray(); 
                 
                // 關閉 
                outStream.close(); 
                is.close(); 
                connect.disconnect(); 
                String address = new String(b,"GBK"); 
                if (address.startsWith("北")||address.startsWith("上")||address.startsWith("重")){ 
                       setCity(address.substring(0,address.indexOf("市"))); 
                  } 
                  if(address.startsWith("香")){ 
                     setCity(address.substring(0,address.indexOf("港"))); 
                  } 
                  if(address.startsWith("澳")){ 
                   setCity(address.substring(0,address.indexOf("門"))); 
                    } 
                    if (address.indexOf("省") != -1) { 
                   setCity(address.substring(address.indexOf("省") + 1, address.indexOf("市"))); 
                    } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
        } 
4、進行天氣查詢實現webservice接口(http://www.webxml.com.cn/webservices/weatherwebservice.asmx)

[java] 
private static final String NAMESPACE = "http://WebXml.com.cn/"; 
 
    // WebService地址 
    private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; 
    private static final String METHOD_NAME = "getWeatherbyCityName"; 
 
    private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; 
    private String weatherToday; 
    private SoapObject detail; 
    private String weatherNow; 
    private String weatherWillBe; 
 
    private void setIcon(String weather, ImageView imageview) { 
        if (weather.equalsIgnoreCase("nothing.gif")) 
            imageview.setBackgroundResource(R.drawable.a_nothing); 
        if (weather.equalsIgnoreCase("0.gif")) 
            imageview.setBackgroundResource(R.drawable.a_0); 
        if (weather.equalsIgnoreCase("1.gif")) 
            imageview.setBackgroundResource(R.drawable.a_1); 
        if (weather.equalsIgnoreCase("2.gif")) 
            imageview.setBackgroundResource(R.drawable.a_2); 
        if (weather.equalsIgnoreCase("3.gif")) 
            imageview.setBackgroundResource(R.drawable.a_3); 
        if (weather.equalsIgnoreCase("4.gif")) 
            imageview.setBackgroundResource(R.drawable.a_4); 
        if (weather.equalsIgnoreCase("5.gif")) 
            imageview.setBackgroundResource(R.drawable.a_5); 
        if (weather.equalsIgnoreCase("6.gif")) 
            imageview.setBackgroundResource(R.drawable.a_6); 
        if (weather.equalsIgnoreCase("7.gif")) 
            imageview.setBackgroundResource(R.drawable.a_7); 
        if (weather.equalsIgnoreCase("8.gif")) 
            imageview.setBackgroundResource(R.drawable.a_8); 
        if (weather.equalsIgnoreCase("9.gif")) 
            imageview.setBackgroundResource(R.drawable.a_9); 
        if (weather.equalsIgnoreCase("10.gif")) 
            imageview.setBackgroundResource(R.drawable.a_10); 
        if (weather.equalsIgnoreCase("11.gif")) 
            imageview.setBackgroundResource(R.drawable.a_11); 
        if (weather.equalsIgnoreCase("12.gif")) 
            imageview.setBackgroundResource(R.drawable.a_12); 
        if (weather.equalsIgnoreCase("13.gif")) 
            imageview.setBackgroundResource(R.drawable.a_13); 
        if (weather.equalsIgnoreCase("14.gif")) 
            imageview.setBackgroundResource(R.drawable.a_14); 
        if (weather.equalsIgnoreCase("15.gif")) 
            imageview.setBackgroundResource(R.drawable.a_15); 
        if (weather.equalsIgnoreCase("16.gif")) 
            imageview.setBackgroundResource(R.drawable.a_16); 
        if (weather.equalsIgnoreCase("17.gif")) 
            imageview.setBackgroundResource(R.drawable.a_17); 
        if (weather.equalsIgnoreCase("18.gif")) 
            imageview.setBackgroundResource(R.drawable.a_18); 
        if (weather.equalsIgnoreCase("19.gif")) 
            imageview.setBackgroundResource(R.drawable.a_19); 
        if (weather.equalsIgnoreCase("20.gif")) 
            imageview.setBackgroundResource(R.drawable.a_20); 
        if (weather.equalsIgnoreCase("21.gif")) 
            imageview.setBackgroundResource(R.drawable.a_21); 
        if (weather.equalsIgnoreCase("22.gif")) 
            imageview.setBackgroundResource(R.drawable.a_22); 
        if (weather.equalsIgnoreCase("23.gif")) 
            imageview.setBackgroundResource(R.drawable.a_23); 
        if (weather.equalsIgnoreCase("24.gif")) 
            imageview.setBackgroundResource(R.drawable.a_24); 
        if (weather.equalsIgnoreCase("25.gif")) 
            imageview.setBackgroundResource(R.drawable.a_25); 
        if (weather.equalsIgnoreCase("26.gif")) 
            imageview.setBackgroundResource(R.drawable.a_26); 
        if (weather.equalsIgnoreCase("27.gif")) 
            imageview.setBackgroundResource(R.drawable.a_27); 
        if (weather.equalsIgnoreCase("28.gif")) 
            imageview.setBackgroundResource(R.drawable.a_28); 
        if (weather.equalsIgnoreCase("29.gif")) 
            imageview.setBackgroundResource(R.drawable.a_29); 
        if (weather.equalsIgnoreCase("30.gif")) 
            imageview.setBackgroundResource(R.drawable.a_30); 
        if (weather.equalsIgnoreCase("31.gif")) 
            imageview.setBackgroundResource(R.drawable.a_31); 
    } 
 
    public void getWeather(String cityName) { 
        try { 
            SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); 
            rpc.addProperty("theCityName", cityName); 
 
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
                    SoapEnvelope.VER11); 
            envelope.bodyOut = rpc; 
            envelope.dotNet = true; 
            envelope.setOutputSoapObject(rpc); 
            HttpTransportSE ht = new HttpTransportSE(URL); 
 
            ht.debug = true; 
 
            ht.call(SOAP_ACTION, envelope); 
            detail = (SoapObject) envelope.getResponse(); 
            parseWeather(detail); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
    private void parseWeather(SoapObject detail) 
            throws UnsupportedEncodingException { 
        textview1 = (TextView) this.findViewById(R.id.TextView01); 
 
        String date = detail.getProperty(6).toString(); 
 
        // 當天天氣 
        weatherToday = "\n天氣:" + date.split(" ")[1]; 
        weatherToday = weatherToday + "\n氣溫:" 
                + detail.getProperty(5).toString(); 
        weatherToday = weatherToday + "\n風力:" 
                + detail.getProperty(7).toString() + "\n"; 
 
        weatherNow = detail.getProperty(8).toString(); 
        weatherWillBe = detail.getProperty(9).toString(); 
 
        textview1.setText(getIp() + '\n' + getCity() + "\n今天" 
                + weatherToday); 
        setIcon(weatherNow, image1); 
        setIcon(weatherWillBe, image2); 
    } 

5、最後在AndroidMainifest.xml加入權限

[java]
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>  
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>  

 


說明:由於筆者水平有限,難免會出現一些錯誤或漏洞,望讀者批評指正。

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved