NullPointerException NullPointerException - 5 months ago 9
Java Question

Which is the easiest approach to get & interpret a XML file response from a PHP script?


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

Answer

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.

Comments