Parsing nested JSON from a server - java

I am working on an android project that parses JSON from a file on a server and converting the data into java objects to display the data using text views.
The JSON file that I am parsing is based on a collection of books. Within each book entry is an author which has nested child elements for the last and first name of that author. Some entries can have multiple authors.
JSON file:
{
"bib": {
"book": [
{
"year": "1994",
"title": "TCP/IP Illustrated",
"author": {
"last": "Stevens",
"first": "W."
},
"publisher": "Addison-Wesley",
"price": "65.95"
},
{
"year": "1992",
"title": "Advanced Programming in the Unix environment",
"author": {
"last": "Stevens",
"first": "W."
},
"publisher": "Addison-Wesley",
"price": "65.95"
},
{
"year": "2000",
"title": "Data on the Web",
"author": [
{
"last": "Abiteboul",
"first": "Serge"
},
{
"last": "Buneman",
"first": "Peter"
},
{
"last": "Suciu",
"first": "Dan"
}
],
"publisher": "Morgan Kaufmann Puslishers",
"price": "39.95"
},
{
"year": "2012",
"title": "Professional Android 4 application development",
"author": {
"last": "Meier",
"first": "Reto"
},
"publisher": "ndianapolis : John Wiley and Sons",
"price": "33.47"
},
{
"year": "2017",
"title": "Java Programming for Beginners: Learn the fundamentals of programming with Java",
"author": {
"last": "Lassoff",
"first": "Mark"
},
"publisher": "Packt Publishing",
"price": "23.99"
},
{
"year": "2005",
"title": "Head First Java",
"author": [
{
"last": "Sierra",
"first": "Kathy"
},
{
"last": "Bates",
"first": "Bert"
},
],
"publisher": "MO'Reilly Media; 2 edition",
"price": "21.25"
},
{
"year": "2013",
"title": "XML for Dummies",
"author": {
"last": "Tittel",
"first": "Ed"
},
"publisher": "Wiley; 4th edition",
"price": "14.99"
},
{
"year": "2019",
"title": "Java XML and JSON: Document Processing for Java SE",
"author": {
"last": "Friesen",
"first": "Jeff"
},
"publisher": "Apress; 2nd ed. edition",
"price": "65.95"
},
{
"year": "2016",
"title": "Java Programming for Android Developers For Dummies (For Dummies (Computers))",
"author": {
"last": "Burd",
"first": "Barry A."
},
"publisher": "John Wiley and Sons; 2nd edition",
"price": "16.99"
}
]
}
}
JSON Parser:
private class parseJSON extends AsyncTask<Void, Void, List<Book>> {
private final String TAG = parseJSON.class.getSimpleName();
#Override
protected List<Book> doInBackground(Void... voids) {
Log.i(TAG, "Start Async to get books.");
ArrayList<Book> bookArray = new ArrayList<>(0);
String jsonUrl = getApplication().getString(R.string.json_feed);
HttpHandler httpHandler = new HttpHandler();
String jsonString = httpHandler.makeJsonServiceCall(jsonUrl);
Log.i(TAG, "Response from url: " + jsonString);
if( jsonString != null) {
try {
JSONObject root = new JSONObject(jsonString);
// Get JSON array node.
JSONArray books = root.getJSONObject("bib").getJSONArray("book");
// Looping through all the books.
for (int i = 0; i < books.length(); i++) {
JSONObject jsonBook = books.getJSONObject(i);
String year = jsonBook.getString("year");
String title = jsonBook.getString("title");
String author = jsonBook.getString("author");
String publisher = jsonBook.getString("publisher");
String price = "£" + jsonBook.getString("price");
final Book bookObject = new Book(year, title, author, publisher, price);
//Add the new books to our result array.
bookArray.add(bookObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return bookArray;
}
#Override
protected void onPostExecute( List<Book> books) {
super.onPostExecute(books);
Log.e(TAG, "Populate UI recycler view with json converted data.");
bookList.setValue(books);
}
}
What is the best way I can accomplish this?

Use Gson by google, in your gradle add:
implementation 'com.google.code.gson:gson:2.8.6'
and you get it like:
Book bookObject = new Gson().fromJson("json", Book.class);

Related

Java - How to iterate json objects keys and values pair?

Here below the json format:
Document{
{Social=4, Productivity=5, Personalization=1, Entertainment=5, Music & Audio=4, Finance=7, Tools=9, Travel & Local=1, Food & Drink=3, Card=1, Photography=5, OTHERS=4, Shopping=4, Maps & Navigation=2, Communication=5, Business=3, Video Players & Editors=3
}}
Here I need only keys no need of values.
I am expecting this kind of output:
{
"name": "Social",
"value": "Social"
},
{
"name": "Productivity",
"value": "Productivity"
}
{
"name": "Personalization",
"value": "Personalization"
}
{
"name": "Entertainment",
"value": "Entertainment"
}
{
"name": "Music & Audio",
"value": "Music & Audio"
}
{
"name": "Finance",
"value": "Finance"
}
{
"name": "Tools",
"value": "Tools"
}
{
"name": "Travel & Local",
"value": "Travel & Local"
}
{
"name": "Food & Drink",
"value": "Food & Drink"
}
{
"name": "Card",
"value": "Card"
}
{
"name": "Photography",
"value": "Photography"
}
{
"name": "OTHERS",
"value": "OTHERS"
}
{
"name": "Shopping",
"value": "Shopping"
}
{
"name": " Maps & Navigation",
"value": " Maps & Navigation"
}
{
"name": "Communication",
"value": "Communication"
}
{
"name": "Business",
"value": "Business"
}
{
"name": "Productivity",
"value": "Productivity"
}
I am not getting a proper solution.
for (Document doc : documents) {
doc.entrySet().forEach(v -> {
SegmentValueOptions inputValues = new SegmentValueOptions();
inputValues.setName(v.getKey());
inputValues.setValue(inputValues.getName());
inputValList.add(inputValues);
});
}
}

Apache Velocity: remove key/value from json

I have a JSON
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2033"
},
"order": {
"amount": 1
}
}
}
}
And I want to remove the card info of it like this:
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"order": {
"amount": 1
}
}
}
}
How can I do this with Apache velocity?
What works is:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.replaceAll("2033","2055"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
This gives me
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2050"
},
"order": {
"amount": 1
}
}
}
}
But now I want to remove the card part but it does not work:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.delete("$.Payload.body.card"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
what am I doing wrong?
Main goal is transform a mapping template in API Gateway for a webhook. The webhook contains to many information and we want to remove some part of the JSON POST call.
Try using the below
#set($dummy=$content.Payload.remove("card"))

how to make count similar id object in list and display in recyclerview?

I have a JSON array, in that I need to make match count using ParentCommentID and parse data into recyclerview using text view while counting i get number count only one position and same for other item also, any help.
{
"d": {
"results": [
{
"__metadata": {
"id": "e7433825-6771-4f5e-96c7-c4d2674d7764",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "f064775c-6161-4bdb-9f4d-8bc6a898d218",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1501,
"ID": 1501,
"Title": null,
"Created": "2019-06-06T04:15:17Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass8C333A77B6564A53BED74CA1BA2D2A10\">
reply add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "e92f708f-93dc-4587-8c4f-5518ed24f360",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "209d2d9a-bb07-4064-aaa0-231ad881a80f",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1500,
"ID": 1500,
"Title": null,
"Created": "2019-06-06T04:14:55Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass44B1A0BB4D314C57BEE20141BFF10491\">comment add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "ec112002-3132-4bc1-8f85-03fbd9fda11d",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "6e8ecb1d-4deb-4168-a8b4-a725abf8002a",
"type": "SP.Data.UserInfoItem"
},
"Title": "Sarada Devi Potti"
},
"Id": 1439,
"ID": 1439,
"Title": "Canceled",
"Created": "2019-06-03T09:32:34Z",
"ParentCommentID": null,
"Comment": "<div class=\"ExternalClass5E1BFEDC348C43719AD940E644E0E0B6\">sdaeadfasdf</div>",
"CommentType": "Public",
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Budget Analyst"
}
]
}
}
My code is onbind method:
Map<String, Integer> commentsCountMap = new HashMap<>();
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the MyDocsResult has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
System.out.println(commentsCountMap);
Log.d("Count:",commentsCountMap.toString());
}
}
for example id 1439, having two ParentCommentID, so in that case for id 1439 there should be a count of two comments, I tried to use for in my
recyclerview adapter but its dose not work.
I think you need to run through the list of Results and when when you find a Result that has a parent comment, then you go in the map, get the parent comment, increment its count by one and stick it back in the Map:
Assuming your Result class is something like this:
class Result {
private String id;
private String parentCommentID;
public Result(String id, String parentCommentID) {
this.id = id;
this.parentCommentID = parentCommentID;
}
// GETTERS/SETTERS
}
And you have a discussion list with 3 Results
discussionsList = Arrays.asList(
new Result("1439", null),
new Result("1500", "1439"),
new Result("1801", "1439")
);
Like your case, 1439 has no parent and 1500 and 1501 both have 1439 as parent
Then you can do this kind of thing:
Map<String, Integer> commentsCountMap = new HashMap<>();
// Loop through the discussion Map
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the Result has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
}
}
System.out.println(commentsCountMap);
This outputs
{1439=2}

Parsing JSON data in java from Facebook Graph response

Here I have a response from the facebook server that returns the list of albums of a selected user. I want to create an array for all the album names labeled "name" as well as the "link" and "cover_photo" to be ready to processed into a listview
{
"data": [
{
"id": "664462156031",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Mobile Uploads",
"link": "https://www.facebook.com/album.php?fbid=664462156031&id=68004222&aid=2081375",
"cover_photo": "754991180141",
"count": 88,
"type": "mobile",
"created_time": "2012-05-12T00:38:14+0000",
"updated_time": "2013-11-04T20:45:08+0000",
"can_upload": false
},
{
"id": "813288706431",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Bitstrips Photos",
"link": "https://www.facebook.com/album.php?fbid=813288706431&id=68004222&aid=1073741826",
"cover_photo": "813288781281",
"count": 1,
"type": "app",
"created_time": "2013-10-31T21:53:11+0000",
"updated_time": "2013-10-31T21:53:15+0000",
"can_upload": false
},
{
"id": "757384214481",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Galveston Vacation 2013",
"description": "Our trip before joining Air Force",
"link": "https://www.facebook.com/album.php?fbid=757384214481&id=68004222&aid=1073741825",
"cover_photo": "757221350861",
"count": 8,
"type": "normal",
"created_time": "2013-05-15T18:37:19+0000",
"updated_time": "2013-05-15T22:12:52+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100002572634186",
"name": "Misty O'Quain"
},
{
"id": "100000582072776",
"name": "Clifford Joyce"
},
{
"id": "1045514613",
"name": "Caity Ellender"
}
],
"paging": {
"cursors": {
"after": "MTA0NTUxNDYxMw==",
"before": "MTAwMDAyNTcyNjM0MTg2"
}
}
}
},
{
"id": "542202136091",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=542202136091&id=68004222&aid=2054735",
"cover_photo": "749743202131",
"count": 20,
"type": "profile",
"created_time": "2010-09-02T04:43:32+0000",
"updated_time": "2013-04-13T13:46:14+0000",
"can_upload": false
},
{
"id": "646032913381",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=646032913381&id=68004222&aid=2079483",
"cover_photo": "681058471881",
"count": 2,
"type": "cover",
"created_time": "2012-03-22T00:25:50+0000",
"updated_time": "2012-07-17T15:26:24+0000",
"can_upload": false
},
{
"id": "599115930391",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Timeline Photos",
"link": "https://www.facebook.com/album.php?fbid=599115930391&id=68004222&aid=2073818",
"cover_photo": "599115935381",
"count": 1,
"type": "wall",
"created_time": "2011-10-20T14:25:23+0000",
"updated_time": "2011-10-20T14:25:23+0000",
"can_upload": false
},
{
"id": "551798524851",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "My Year - 2010",
"link": "https://www.facebook.com/album.php?fbid=551798524851&id=68004222&aid=2060899",
"cover_photo": "551798544811",
"count": 1,
"type": "normal",
"created_time": "2010-12-19T05:11:51+0000",
"updated_time": "2010-12-19T05:12:14+0000",
"can_upload": false
},
{
"id": "522841060841",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "Night Lightnin",
"description": "Always wanted to do this, because lightning is way cooler at night. Perhaps next time I'll be able to refine my methods a little. Read as \"bring tripod so you don't have to prop cam up on roof of car\"",
"location": "Just north of Sulphur",
"link": "https://www.facebook.com/album.php?fbid=522841060841&id=68004222&aid=2036447",
"cover_photo": "522841195571",
"count": 15,
"type": "normal",
"created_time": "2009-09-16T04:38:43+0000",
"updated_time": "2009-09-16T04:42:32+0000",
"can_upload": false
},
{
"id": "513270939441",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "The Stuff That Doesn't Make Another Album",
"description": "See title.",
"location": "Good question",
"link": "https://www.facebook.com/album.php?fbid=513270939441&id=68004222&aid=2029083",
"cover_photo": "513271029261",
"count": 9,
"type": "normal",
"created_time": "2009-03-03T06:43:09+0000",
"updated_time": "2009-04-01T02:20:22+0000",
"can_upload": false,
"comments": {
"data": [
{
"id": "513270939441_3444",
"from": {
"name": "Misty Bylsma Royal",
"id": "592607110"
},
"message": "I love how the beautiful lady pile is in the same album as the bull pics. Nice.",
"can_remove": false,
"created_time": "2009-03-03T22:37:18+0000",
"like_count": 0,
"user_likes": false
},
{
"id": "513270939441_3451",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"message": "Only because they are both things that i only had a few pictures of.",
"can_remove": false,
"created_time": "2009-03-04T02:46:11+0000",
"like_count": 0,
"user_likes": false
}
],
"paging": {
"cursors": {
"after": "Mg==",
"before": "MQ=="
}
}
}
},
{
"id": "511605661671",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "What the...?",
"description": "What's wrong with the rain? It's flaky and piles up on stuff... Did the plants leak something?\n",
"location": "Outside",
"link": "https://www.facebook.com/album.php?fbid=511605661671&id=68004222&aid=2026641",
"cover_photo": "511605681631",
"count": 54,
"type": "normal",
"created_time": "2008-12-11T11:59:27+0000",
"updated_time": "2008-12-13T18:06:00+0000",
"can_upload": false
},
{
"id": "511565886381",
"from": {
"name": "Nate Ellender",
"id": "68004222"
},
"name": "The Christmas Tree Farm",
"description": "An Ellender Tradition",
"location": "Grant",
"link": "https://www.facebook.com/album.php?fbid=511565886381&id=68004222&aid=2026590",
"cover_photo": "511566170811",
"count": 19,
"type": "normal",
"created_time": "2008-12-09T02:38:54+0000",
"updated_time": "2008-12-09T02:55:21+0000",
"can_upload": false
}
],
"paging": {
"cursors": {
"after": "NTExNTY1ODg2Mzgx",
"before": "NjY0NDYyMTU2MDMx"
}
}
}
Also, here is where the json data is returned (response). I will need to start parsing the data under response.
Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
Session.getActiveSession().requestNewReadPermissions(np);
Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
}
});
rq.executeAsync();
Any example would be helpful. I would like to study the code to get a better understanding on parsing json data.
You can get a good example of how to do here: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Basically you have to use JSONObject and JSONArray, that you can get from getJSONObject() and getJSONArray().
Once you're on the good spot, use getJSONString() to get the desired value.
In your case it will be something like this:
JSONObject json = new JSONObject(response)
JSONArray jarray = json.getJSONArray("data");
for(int i = 0; i < jarray.length(); i++){
JSONObject oneAlbum = jarray.getJSONObject(i);
//get your values
oneAlbum.getJSONString("name"); // this will return you the album's name.
}
Hope this will help you.
Try this
Session.NewPermissionsRequest np = new Session.NewPermissionsRequest(this, "friends_photos");
Session.getActiveSession().requestNewReadPermissions(np);
Request rq = new Request(Session.getActiveSession(), userID + "/albums", null, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
JSONArray albumArr = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
for (int i = 0; i < albumArr.length(); i++) {
JSONObject item = albumArr.getJSONObject(i);
System.out.println("id : " + item.getString("id"));
System.out.println("fromName : " + item.getJSONObject("from").getString("name"));
System.out.println("fromid : " + item.getJSONObject("from").getString("id"));
System.out.println("link : " + item.getString("link"));
System.out.println("cover_photo : " + item.getString("cover_photo"));
System.out.println("count : " + item.getString("count"));
System.out.println("created_time : " + item.getString("created_time"));
System.out.println("updated_time : " + item.getString("updated_time"));
System.out.println("can_upload : " + item.getString("can_upload"));
}
}
});
rq.executeAsync();

Parse Json String (Feed RSS file) in Android

How can I parse this piece of JSON code?
{
"direction": "ltr",
"id": "feed/http => //www.theverge.com/rss/full.xml",
"title": "The Verge - All Posts",
"continuation": "CLKM0OyU0rYC",
"self": [
{
" href": "https => //cloud.feedly.com/reader/3/stream/contents/feed%2Fhttp%3A%2F%2Fwww.theverge.com%2Frss%2Ffull.xml?n=20&unreadOnly=true"
}
],
"alternate": [
{
"href": "http://www.theverge.com/",
"type": "text/html"
}
],
"updated": 1367539068016,
"items": [
{
"id": "entryId",
"unread": true,
"categories": [
{
"id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/tech",
"label": "tech"
}
],
"tags": [
{
"id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/tag/inspiration",
"label": "inspiration"
}
],
"title": "NBC's reviled sci-fi drama 'Heroes' may get a second lease on life as Xbox Live exclusive",
"published": 1367539068016,
"updated": 1367539068016,
"crawled": 1367539068016,
"alternate": [
{
"href": "http://www.theverge.com/2013/4/17/4236096/nbc-heroes-may-get-a-second-lease-on-life-on-xbox-live",
"type": "text/html"
}
],
"content": {
"direction": "ltr",
"content": "..."
},
"author": "Nathan Ingraham",
"origin": {
"streamId": "feed/http://www.theverge.com/rss/full.xml",
"title": "The Verge - All Posts",
"htmlUrl": "http://www.theverge.com/"
},
"engagement": 15
},
{
"id": "entryId2",
"unread": true,
"categories": [
{
"id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/tech",
"label": "tech"
}
],
"tags": [
{
"id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/tag/inspiration",
"label": "inspiration"
}
],
"title": "Senate rejects bipartisan gun control measure for background checks despite broad public support",
"published": 1367539068016,
"updated": 1367539068016,
"crawled": 1367539068016,
"alternate": [
{
"href": "http://www.theverge.com/2013/4/17/4236136/senate-rejects-gun-control-amendment",
"type": "text/html"
}
],
"content": {
"direction": "ltr",
"content": "...html content..."
},
"author": "T.C. Sottek",
"origin": {
"streamId": "feed/http://www.theverge.com/rss/full.xml",
"title": "The Verge - All Posts",
"htmlUrl": "http://www.theverge.com/"
},
"engagement": 39
}
]
}
That is my solution but it doesn't work... what is my error? thanks
try{
//JSONArray elements = new JSONArray (response);
JSONObject json=new JSONObject(response);
JSONArray elements = json.getJSONArray("items");
Log.d(TAG, "Elemenenti numero" +elements.length());
// Getting Array of Contacts
// looping through All Contacts
for(int i = 0; i < elements.length(); i++){
JSONObject c = elements.getJSONObject(i);
// Storing each json item in variable
String identifier = c.getString("id");
String title = c.getString("title");
String link = c.getString("originId");
String data = c.getString("published");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date=new Date();
try {
date = format.parse(data);
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject summaryObj= c.getJSONObject("summary");
String summary = summaryObj.getString("content");
JSONObject contentObj= c.getJSONObject("content");
String content = contentObj.getString("content");
JSONObject sourceObj= c.getJSONObject("origin");
String source = contentObj.getString("title");
if (summary.length()==0 && content.length()!=0) summary=content;
if (content.length()==0 && summary.length()!=0) content=summary;
String image=this.getFirstImage(content);
FeedItem toAdd=new FeedItem(identifier, title, link, date, null, summary, content, image, source);
toAdd.toString();
}
}catch (JSONException e) {
e.printStackTrace();
}
JSONObject summaryObj= c.getJSONObject("summary");
There is no element called summary, you may try
if(c.has("summary")) {
JSONObject summaryObj= c.getJSONObject("summary");
}
if that doesn't work, please post your stacktrace, (logcat)
You don't have any tag named "originID". but you are trying to get String from it.
Similarly,you don't have tag "summary" also but you are trying to get JSONObject from it.

Categories