I'm writing an android reader app that pulls content from a wordpress.com site using the wordpress REST API, which returns JSON objects that I am deserializing into Article objects that are defined in the app. The following code, which gets data for a single post, works correctly:
private class getOne extends AsyncTask <Void, Void, JSONObject> {
private static final String url = "https://public-api.wordpress.com/rest/v1/sites/drewmore4.wordpress.com/posts/slug:good-one";
@Override
protected JSONObject doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("accept", "application/json");
HttpResponse response;
JSONObject object = new JSONObject();
String resprint = new String();
try {
response = httpclient.execute(httpget);
// Get the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// get entity contents and convert it to string
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
resprint = result;
// construct a JSON object with result
object=new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
}
}
catch (ClientProtocolException e) {System.out.println("CPE"); e.printStackTrace();}
catch (IOException e) {System.out.println("IOE"); e.printStackTrace();}
catch (JSONException e) { System.out.println("JSONe"); e.printStackTrace();}
return object;
}
@Override
protected void onPostExecute (JSONObject object){
System.out.println("POSTStexxx");
Gson gson = new Gson();
Article a = gson.fromJson(object.toString(), Article.class);
System.out.println("XXCONTENT: " + a.content);
System.out.println(a.ID);
System.out.println(a.title);
System.out.println(a.author.name);
// System.out.println(a.attachments.URL);
WebView wv = (WebView)findViewById(R.id.mainview);
wv.loadDataWithBaseURL(url, a.content, "text/html", "UTF-8", null);
wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
}
}
The println statements show the expected results, confirming that the object has been deserialized properly. The following code, which should get data from all posts on the site, is not working properly:
private class getAll extends AsyncTask <Void, Void, JSONObject> {
private static final String url = "https://public-api.wordpress.com/rest/v1/sites/drewmore4.wordpress.com/posts/";
@Override
protected JSONObject doInBackground(Void... params) {
//set up client and prepare request object to accept a json object
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("accept", "application/json");
JSONObject returned = new JSONObject();
HttpResponse response;
String resprint = new String();
try {
response = httpclient.execute(httpget);
// Get the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// get entity contents and convert it to string
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
resprint = result;
// construct a JSON object with result
returned =new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
}
}
catch (ClientProtocolException e) {System.out.println("CPE"); e.printStackTrace();}
catch (IOException e) {System.out.println("IOE"); e.printStackTrace();}
catch (JSONException e) { System.out.println("JSONe"); e.printStackTrace();}
// stories = object;
return returned;
}
@Override
protected void onPostExecute (JSONObject returned){
System.out.println("POSTStexxx");
Gson gson = new Gson();
PostsHandler ph = gson.fromJson(returned.toString(), PostsHandler.class);
System.out.println("WAKAWAKA: " + ph.posts.length);
// System.out.println("ARRAYLENGTH" + ja.length());
ArrayList<Article> arts = new ArrayList<Article>();
for (JSONObject o : ph.posts) {
Article a = gson.fromJson(o.toString(), Article.class);
System.out.println("TITLE: " + a.title);
System.out.println("TITLE: " + a.author);
arts.add(a);
}
System.out.println("ARTICLEARRAY: " + arts.size());
stories = arts;
populateUI();
}
The JSON object returned here contains a JSONArray of objects identical to the one returned by a query for a single post. The program runs, and one of the println statements here shows that the size of the arraylist is correct (i.e. matches the expected number of posts), but the fields for each object (title, author, etc) are null. I'm guessing I'm not treating the array properly, but I don't know where I'm erring. Here is the Article class, which maps each post object:
public class Article implements Serializable {
// private static final long serialVersionUID = 1L;
int ID;
public String title;
public String excerpt;
public Author author;
public String date;
public String URL;
@SerializedName("featured_image")
public String image;
public String content;
//public String[] attachments;
public Attachment attachments;
public int comment_count;
public int like_count;
}
class Author {
long id;
String name;
String URL;
}
And the PostsHandler class, to which the response to the query for all posts is mapped (and where I suspect my problem is):
public class PostsHandler {
int number;
JSONObject[] posts;
}
All fields not marked with the @SerializedName annotation are identical to the ones used in the JSONObjects.
The JSONObjects I'm working with can be seen at: