Reputation: 1456
I've tried to imitate the chosen answers in this sort of problem but I'm unsure why I'm failing to retrieving the URLs of the "attachments" in this.
What I'm after is a way to get every "post" and then grab every "attachment" image URL to save as a string. I've tried doing this on my emulator but it just stalls and runs forever. For certain reasons I am unable to use my real phone as a debugger too or else I would post a logcat.
One thing I am certain is that everything, minus the attachments, is coming in correctly. I've managed to get the posts downloading but cannot get anything thats nested. I'm newer to JSON so any help is very appreciated.
My Async:
// you can make this class as another java file so it will be separated from your main activity.
// https://www.codeofaninja.com/2013/11/android-json-parsing-tutorial.html
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
private ArrayList<RssFeedItem> tempArray = new ArrayList<RssFeedItem>();
final String TAG = "AsyncTaskParseJson";
private ProgressDialog progress;
// set your json string url here
String yourJsonStringUrl = "http://www.prindlepost.org/?json=tag_slug=java";
// contacts JSONArray
JSONArray dataJsonArr = null;
JSONArray imageURLArr = null;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(getActivity());
progress.setTitle("Downloading Prindle's Posts");
progress.setMessage("This should just take a moment.");
progress.show();
}
@Override
protected String doInBackground(String... arg0)
{
try
{
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("posts");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++)
{
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
String type = c.getString("type");
String slug = c.getString("slug");
String title = c.getString("title");
String content = c.getString("content");
String author = c.getString("author");
//http://stackoverflow.com/questions/19748829/android-get-json-array-nested-in-array
JSONObject attachments = c.getJSONObject("attachments");
Log.d("attachment",""+attachments.getString("url"));
// show the values in our logcat
Log.e(TAG, "id: " + id
+ ", type: " + type
+ ", slug: " + slug
+ ", title: " + title
+ ", author: " + author
+ ", content: " + content + "\n\n");
tempArray.add(new RssFeedItem(title, content, "", 0, new Date(), author));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
The JSON: http://www.prindlepost.org/?json=tag_slug=java
{
status: "ok",
count: 10,
count_total: 334,
pages: 34,
posts: [
{
id: 4230,
type: "post",
slug: "crowdsourcing-justice",
url: "http://www.prindlepost.org/2015/06/crowdsourcing-justice/",
status: "publish",
title: "Crowdsourcing Justice",
title_plain: "Crowdsourcing Justice",
content: "<p>The video begins abruptly. Likely recorded on a phone, the footage is shaky and blurry, yet the subject is sickeningly unmistakeable: a crying infant being repeatedly and violently dunked into a bucket of water. First it is held by the arms, then upside down by one leg, then grasped by the face as an unidentified woman pulls it through the water. Near the end of the video, the infant falls silent, the only remaining audio the splashing of water and murmured conversation as the child is dunked again and again.</p> <div class="more-link-wrap wpb_button"> <a href="http://www.prindlepost.org/2015/06/crowdsourcing-justice/" class="more-link">Read more</a></div> ",
excerpt: "<p>Facebook’s decision not to censor a video of child abuse poses questions of censorship, activism and online justice. </p> ",
date: "2015-06-09 14:00:19",
modified: "2015-06-10 09:53:36",
categories: [
{
id: 433,
slug: "crime-and-law",
title: "Crime and Law",
description: "",
parent: 63,
post_count: 14
},
{
id: 38,
slug: "ethics-news",
title: "Ethics News",
description: "",
parent: 0,
post_count: 153
},
{
id: 63,
slug: "society-ethics-news",
title: "Society",
description: "",
parent: 38,
post_count: 187
}
],
tags: [
{
id: 180,
slug: "abuse",
title: "abuse",
description: "",
post_count: 2
},
{
id: 481,
slug: "child-abuse",
title: "child abuse",
description: "",
post_count: 1
},
{
id: 482,
slug: "doxxing",
title: "doxxing",
description: "",
post_count: 1
},
{
id: 57,
slug: "facebook",
title: "Facebook",
description: "",
post_count: 4
},
{
id: 470,
slug: "internet",
title: "internet",
description: "",
post_count: 2
},
{
id: 130,
slug: "justice",
title: "justice",
description: "",
post_count: 2
},
{
id: 59,
slug: "social-media",
title: "social media",
description: "",
post_count: 4
}
],
author: {
id: 43,
slug: "connergordon_2016",
name: "Conner Gordon",
first_name: "Conner",
last_name: "Gordon",
nickname: "connergordon_2016",
url: "http://connergordon.tumblr.com",
description: "Conner is a web and social media intern at the Prindle Institute. A Political Science and English double major from Carmel, Indiana, Conner's ethical interests lie in memory studies, conflict analysis and the ethics of representation. He also has interests in literature, art and photography."
},
comments: [ ],
attachments: [
{
id: 4233,
url: "http://www.prindlepost.org/wp-content/uploads/2015/06/Screen-Shot-2015-06-09-at-11.48.59-AM.png",
slug: "screen-shot-2015-06-09-at-11-48-59-am",
title: "",
description: "",
caption: "Image credit: Screenshot from Youtube",
parent: 4230,
mime_type: "image/png",
images: [ ]
},
{
id: 4235,
url: "http://www.prindlepost.org/wp-content/uploads/2015/06/Screen-Shot-2015-06-09-at-11.48.59-AM1.png",
slug: "screen-shot-2015-06-09-at-11-48-59-am-2",
title: "",
description: "",
caption: "<a href="https://www.youtube.com/watch?v=UOCB8j6QgeI&bpctr=1433876092">Image/Youtube</a>",
parent: 4230,
mime_type: "image/png",
images: [ ]
}
],
Upvotes: 1
Views: 127
Reputation: 320
I had the same problem. After a few days melting my brain, I tried using Google's GSON. It does all the parsing and thinking for you, and returns a nice little object with all the information from the JSON.
Here's the project link: https://github.com/google/gson
To use it, you have to instantiate a new Gson
parser, like so
Gson gson = new Gson();
YourObject object = gson.fromJson(jsonString, YourObject.class);
And the YourObject
class should look something like this:
public class YourObject{
int status;
int count;
String count_total;
...
Post[] posts;
}
Now you create a Post
class with the fields predicted in your JSON:
public class Post{
int id;
String type;
String slug;
...
Category[] categories;
}
I think you can get an idea on how to set up your POJO's. Keep in mind that, if you are getting an array of objects as your base object in JSON, be sure to use YourObject[]
instead of YourObject
when calling gson.fromJson
Just a heads-up: If any of the Json elements have a null
or an empty value, even though they are primarily an int
in your YourObject
class, it is best to declare them as String
to avoid java.lang.NumberFormatException
.
Upvotes: 2