V
V
Vladislav Bogdanov2013-02-18 12:44:23
Android
Vladislav Bogdanov, 2013-02-18 12:44:23

How to write a program to send a request to a given site address, receive an xml file and extract data from it?

How to write a program to send a request to a given site address, receive an xml file and extract data from it? Please describe in detail if you can. If you can at least part of this process fully explain.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
palmut, 2013-02-18
@Paradoxxx

A question from the category - how to write a program for Android. :)
The scheme is as follows:
1. Form a request 2. If successful, launch the
parser
3. Return data
Looks like this:

  private void downloadContent() {
    HttpURLConnection connection = null;
    BufferedInputStream ins = null;
    String urlString = "<заданный адрес сайта>";

    try {
      URL url = new URL(urlString);

      connection = (HttpURLConnection) url.openConnection();
      int status = connection.getResponseCode();
      if (status == HttpURLConnection.HTTP_OK) {
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        ins = new BufferedInputStream(connection.getInputStream());
        parser.setInput(ins, null);
        parseData(parser);
      }

    } catch (Exception e) {
      // надо обработать исключение
    } finally {
      if (ins != null) {
        try {
          ins.close();
        } catch (IOException e) {}
      }
    }

  }
  
  public void parseData(XmlPullParser parser) throws XmlPullParserException, IOException {

    int eventType = parser.getEventType();
    final String xmlTag = "rootTAG";

    do {
      if (eventType == XmlPullParser.START_TAG) {
        String name = parser.getName();
        if (xmlTag.equalsIgnoreCase(name) && (parser.getAttributeCount() > 0)) {
          for (int i = 0, N = parser.getAttributeCount(); i < N; i++) {
            // парсим атрибуты
            parseAttribute(parser.getAttributeName(i), parser.getAttributeValue(i));
          }
        }
        // парсим вложенный таг
        parseTag(parser);
      }
      eventType = parser.next();
    } while (!(eventType == XmlPullParser.END_TAG && xmlTag.equals(parser.getName()))
            && (eventType != XmlPullParser.END_DOCUMENT));

  }


Ask questions - I will open the topic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question