如何访问网站的内容

时间:2013-01-13 16:34:33

标签: android android-internet

我想在Android中制作PNR状态应用程序,那么有人可以告诉我如何访问印度铁路网站吗?我可以使用以下技术访问Android中的互联网资源吗?

private void refreshEarthquakes() {
    // Get the XML
    URL url;
    try {
        String quakeFeed = getString(R.string.quake_feed);
        url = new URL(quakeFeed);

        URLConnection connection;
        connection = url.openConnection();

        HttpURLConnection httpConnection = (HttpURLConnection)connection;
        int responseCode = httpConnection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream in = httpConnection.getInputStream();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            // Parse the earthquake feed.
            Document dom = db.parse(in);
            Element docEle = dom.getDocumentElement();

            // Clear the old earthquakes
            earthquakes.clear();

            // Get a list of each earthquake entry.
            NodeList nl = docEle.getElementsByTagName("entry");
            if (nl != null && nl.getLength() > 0) {
                for (int i = 0 ; i < nl.getLength(); i++) {
                    Element entry = (Element)nl.item(i);
                    Element title = (Element)entry.getElementsByTagName("title").item(0);
                    Element g = (Element)entry.getElementsByTagName("georss:point").item(0);
                    Element when = (Element)entry.getElementsByTagName("updated").item(0);
                    Element link = (Element)entry.getElementsByTagName("link").item(0);

                    String details = title.getFirstChild().getNodeValue();
                    String hostname = "http://earthquake.usgs.gov";
                    String linkString = hostname + link.getAttribute("href");

                    String point = g.getFirstChild().getNodeValue();
                    String dt = when.getFirstChild().getNodeValue(); 
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
                    Date qdate = new GregorianCalendar(0, 0, 0).getTime();
                    try {
                        qdate = sdf.parse(dt);
                    } catch (ParseException e) {
                        Log.d(TAG, "Date parsing exception.", e);
                    }

                    String[] location = point.split(" ");
                    Location l = new Location("dummyGPS");
                    l.setLatitude(Double.parseDouble(location[0]));
                    l.setLongitude(Double.parseDouble(location[1]));

                    String magnitudeString = details.split(" ")[1];
                    int end =  magnitudeString.length()-1;
                    double magnitude = Double.parseDouble(magnitudeString.substring(0, end));

                    details = details.split(",")[1].trim();

                    Quake quake = new Quake(qdate, details, l, magnitude, linkString);

                    // Process a newly found earthquake
                    addNewQuake(quake);
                }
            }
        }
    } catch (MalformedURLException e) {
        Log.d(TAG, "MalformedURLException", e);
    } catch (IOException e) {
        Log.d(TAG, "IOException", e);
    } catch (ParserConfigurationException e) {
        Log.d(TAG, "Parser Configuration Exception", e);
    } catch (SAXException e) {
        Log.d(TAG, "SAX Exception", e);
    } finally {
    }
}

private void addNewQuake(Quake _quake) {
    // Add the new quake to our list of earthquakes.
    earthquakes.add(_quake);

    // Notify the array adapter of a change.
    aa.notifyDataSetChanged();
}

0 个答案:

没有答案