-1

Possible Duplicate:
Best XML Parser for PHP
Using JAXB with Google Android

I need to add a simple functionality into my Android app.

I must connect to this fictional script: http://test/getMagazinesList.php

The script will return me a XML file like this:

<magazines>
 <magazine title="APP 1" id="1">
  <description>Prueba real</description>
   <miniature>http://web/miniature.jpg</miniature>
 </magazine>
 <magazine title="APP 2" id="2">
  <description>Prueba real 2</description>
   <miniature>http://web/miniature2.jpg</miniature>
 </magazine>
 <magazine title="APP 3" id="3">
  <description>Prueba real 3</description>
   <miniature>http://web/miniature3.jpg</miniature>
 </magazine>
</magazines>

The objective is to store all the info of the XML in a List of these MagazinePreview objects:

public class MagazinePreview {
String title;
String id;
String description;
String miniatureUrl;
Bitmap miniature;
public MagazinePreview(String title, String id, String description,
        String miniatureUrl) {
    super();
    this.title = title;
    this.id = id;
    this.description = description;
    this.miniatureUrl = miniatureUrl;
    }   
}

Which is the easiest approach to connect to that PHP and get the XML and interpret it storing the info on the List of MagazinePreview class objects?

Thanks

Community
  • 1
  • 1
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • 4
    and many more suitable duplicates in the Related section to the right. Please use the search function before asking. [Do your homework.](http://stackoverflow.com/questions/ask-advice) – Gordon Mar 15 '12 at 09:05
  • 1
    You can refer here too. http://stackoverflow.com/questions/4624440/sending-a-response-from-php-to-an-android-java-mobile-app – Calvin Mar 15 '12 at 09:08
  • 3
    @Pableras84 "Easiest way" is not constructive. As far as I can tell the OP is simply asking how to process XML and we got answers to that for any language. – Gordon Mar 15 '12 at 09:09
  • mention of PHP is irrelevant, here – njzk2 Mar 15 '12 at 09:18

1 Answers1

2

You can use Simple XML for this.

Adorn your MagazinePreview class with these annotations:

public class MagazinePreview 
{
  @Attribute
  String title;
  @Attribute
  String id;
  @Element
  String description;
  @Element(name="miniatura")
  String miniatureUrl;
  Bitmap miniature;
  public MagazinePreview(String title, String id, 
                         String description, String miniatureUrl) 
  {
    super();
    ...
  }   
}

Now add a class for your collection of magazines. Add a method that performs deserialization of an XML string, instantiating your objects as it does so:

@Root
public class MagazinePreviews
{
  @ElementList(name="magazines")
  ArrayList<MagazinePreviews> previews;

  public static MagazinePreviews Load(String xml)
  {
    Serializer serializer = new Persister();
    return serializer.read(MagazinePreviews.class, xml);
  }
}

This will get you a MagazinePreviews instance with your MagazinePreview instances in it. Simple XML does all the heavy lifting. It will use the annotations (@Element, @Attribute) to know which data to place in which fields.

You'll still need a way to connect to your PHP server script. Assuming it's a script that simply returns a string of XML, you could use this:

public String getXML() throws Exception
{
  StringBuilder builder = new StringBuilder();
  HttpClient httpclient = new DefaultHttpClient();
  HttpGet httpget = new HttpGet("myscript.php");
  HttpResponse response = httpclient.execute(httpget);
  int statuscode = response.getStatusLine().getStatusCode();
  if(statuscode == 200)
  {
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    String line;
    while ((line = reader.readLine()) != null)  builder.append(line);               
  }
  else throw new Exception("HTTP error: " + String.valueOf(statuscode));
  return builder.toString();
}

This code calls your PHP script on the server, retrieves the response (assumed to be an XML string), or throws an error if the request fails.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37