Q:

Fill Listview from XML obtained from HTTP POST

I'm trying to populate a ListView with the data obtained using HTTP POST URL, the data received is a XML document like this: 
     <?xml version="1.0" encoding="utf-8" ?> 
- <Arrives>
- <Arrive>
  <IdStop>226</IdStop> 
  <idLine>70</idLine> 
  <IsHead>True</IsHead> 
  <Destination>ALSACIA</Destination> 
  <IdBus>0000</IdBus> 
  <TimeLeftBus>0</TimeLeftBus> 
  <DistanceBus>373</DistanceBus> 
  <PositionXBus>-1</PositionXBus> 
  <PositionYBus>-1</PositionYBus> 
  <PositionTypeBus>2</PositionTypeBus> 
  </Arrive>
- <Arrive>
  <IdStop>226</IdStop> 
  <idLine>11</idLine> 
  <IsHead>True</IsHead> 
  <Destination>BARRIO BLANCO</Destination> 
  <IdBus>0000</IdBus> 
  <TimeLeftBus>161</TimeLeftBus> 
  <DistanceBus>1498</DistanceBus> 
  <PositionXBus>-1</PositionXBus> 
  <PositionYBus>-1</PositionYBus> 
  <PositionTypeBus>1</PositionTypeBus> 
  </Arrive>
- <Arrive>
  <IdStop>226</IdStop> 
  <idLine>N3</idLine> 
  <IsHead>True</IsHead> 
  <Destination>CIBELES</Destination> 
  <IdBus>0000</IdBus> 
  <TimeLeftBus>422</TimeLeftBus> 
  <DistanceBus>1923</DistanceBus> 
  <PositionXBus>-1</PositionXBus> 
  <PositionYBus>-1</PositionYBus> 
  <PositionTypeBus>1</PositionTypeBus> 
  </Arrive>
  </Arrives>

The only tags which I need to use in ListView are idLine, Destination and TimeLeftBus.
The method I planned to use in HTTP Request: (I can do this using GET METHOD too) 
    public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("SITE URL");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("apiid", "API ID"));
        nameValuePairs.add(new BasicNameValuePair("apikey", "API KEY"));
        nameValuePairs.add(new BasicNameValuePair("stopid", "STOP ID"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Log exception
    } catch (IOException e) {
        // TODO Log exception
    }
} 

How can implement this?  Thanks for your help.  

A:

I'm not really familiar with obtaining data from an HTTP POST URL but I do know how to get your xml data to your list view. What I would do is 

Create an object to encapsulate idLine, Destination and TimeLeftBus 
Use an xml parser to return a list of those objects 
Create a custom adapter (if necessary) to populate your list view. 

See the code below for each of these steps.
Step 1:
public class ArrivalInfo {

private String idLine;
private String destination;
private String timeLeft;

public void setIdLine(String id) {
this.idLine = id;
}

public String getIdLine() {
     return idLine;
}
...(getters and setters for other fields)
}

Step 2:
public class XMLParser {

//the tags as found in your XML document
private static final String ARRIVE = "arrive";
private static final String ID_LINE = "idLine";
private static final String DEST = "Destination";
private static final String TIME_LEFT_BUS = "TimeLeftBus";

private ArrivalInfo curArrival = null; 

List<ArrivalInfo> arrivals = new ArrayList<ArrivalInfo>();

public List<ArrivalInfo> getArrivals() {
      return arrivals; 
}

public void parseXml() {

try {

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    InputStream stream = getResources().openRawResource(...) (this can be done in different
      ways depending on where the file is)
    int eventType = xpp.getEventType();
    while(eventType != XmlPullParser.END_DOCUMENT) {

         if(eventType == XmlPullParser.START_TAG) {
            handleStartTag(xpp.getName());
         } 
         else if (eventType == XmlPullParser.END_TAG) {
            handleEndTag(xpp.getName());
         }
         else if (eventType == XmlPullParser.TEXT) {
            handleText(xpp.getName(), xpp.getText());
         }
         eventType = xpp.next();
   } catch(NotFoundException e) {
       ...handle exeptions
   }

   public void handleStartTag(String tag) {
     if(tag.equalsIgnoreCase(ARRIVE)) {
       curArrival = new ArrivalInfo();
     }
   }

   public void handleEndTag(String tag) {
      if(tag.equalsIgnoreCase(ARRIVE)) {
         arrivals.add(curArrival);
      }
   }

   public void handleText(String tag, String text) {
      if(curArrival != null) {

         if(tag.equalsIgnoreCase(ID_LINE)) {
            curArrival.setIdLine(text);
         }
         else if (tag.equalsIgnoreCase(DEST)) {
            curArrival.setDestination(text);
         } 
       // and so on... 
      }
   } 

} //end of XmlParser

Step 3: (This would happen in a non UI thread, i.e. use an AsyncTask to do this on a different thread);
public class MyActivity extends Activity {

 private ListView mListView;
 private StableArrayAdapter adapter; 
 private List<ArrivalInfo> arrivals; 

 private class LoadDataTask extends AsyncTask<Void, Void, List<ArrivalInfo>>() {

    @Override 
    public void doInBackground(Void...params) {
      XmlParser parser = new XmlParser();
      try {
        parser.parseXml(); 
      } finally {
         return parser.getArrivals();
      }
    }

    @Override
    public void onPostExecute(List<ArrivalInfo> list) {

     if(mListView != null && adapter != null) {

        arrivals = list; 
        adapter.notifyDataSetChanged(); 

     }

   }
 } // end of loading task. 

 @Override
 public void onCreate(Bundle savedInstanceState) {

     arrivals = new ArrayList<ArrivalInfo>();

     setContentView(R.layout.my_layout);
     mListView = findViewById(android.R.id.list);

     StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, arrivals);

     mListView.setAdapter(adapter);

     new LoadDataTask().execute();

 }

} // end of your activity

And that's pretty much it! hope that helps you do what you want to do. 

