1. 程式人生 > >Android客戶端解析web伺服器XML資料小問題

Android客戶端解析web伺服器XML資料小問題

Android客戶端解析web伺服器XML資料

通過Pull 解析方式實現

程式碼如下:

</pre><pre code_snippet_id="1703605" snippet_file_name="blog_20160601_4_6197354" name="code" class="java">private void sendXMLtWithHttpURLConnection() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection con = null;
                try {

                    URL url = new URL("http://192.168.1.107:8062/get_data.xml");
                    con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("GET");
                    con.setConnectTimeout(8000);
                    con.setReadTimeout(8000);
                    int responseCode = con.getResponseCode();
                    if (responseCode == 200) {
                        InputStream in = con.getInputStream();
                        parseXML(in);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        }).start();

    }
    private void parseXML(InputStream in) {
        XmlPullParser pullParser = Xml.newPullParser();
        try {
            pullParser.setInput(in, "UTF-8");
            int eventType = pullParser.getEventType();
            String id = "";
            String name = "";
            String version = "";
            while (eventType != pullParser.END_DOCUMENT) {
                String nodeName = pullParser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG: {
                        if ("id".equals(nodeName)) {
                            id = pullParser.nextText();
                        }
                        if ("name".equals(nodeName)) {
                            name = pullParser.nextText();
                        }
                        if ("version".equals(nodeName)) {
                            version = pullParser.nextText();
                        }
                        break;
                    }
                    case XmlPullParser.END_TAG: {
                        if ("app".equals(nodeName)) {
                            Log.d("MainActivity", "id:" + id);
                            Log.d("MainActivity", "name:" + name);
                            Log.d("MainActivity", "version:" + version);
                        }
                        break;
                    }
                    default:
                        break;
                }
                eventType = pullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

期間xml的訪問報一個下面這樣的錯

java.net.ConnectException: failed to connect to /127.0.0.1 (port 8062) after 8000ms: isConnected failed: ECONNREFUSED (Connection refused)

加上許可權檢視程式碼找了很久發現不了原因,經過顯示測試,原來是訪問不到這個頁面

但是在電腦上面是可以訪問到的

查了下搜尋引擎原來要保證兩者之間處於同一網段

好像是模擬器可以用10.0.2.2代替

我用的真機測試要改為ip地址就沒有報錯了。

localhost或者127.0.0.1是不能訪問的,會報以上這個錯誤。