Download Together With Parse Blogspot Json Feed

Posted on

This instance present how to download together with parse blogspot JSON feed.

The url of my feed is https://rootandroid.org//feeds/posts/default?alt=json.

Actually I don’t know the format of blogspot json feed, I role Online JSON Viewer (http://jsonviewer.stack.hu/) to guess. This video present how:


In my example, I download together with parse the json feed inwards AsyncTask, Only post service championship together with url retrieved, together with display every moment String inwards ListView.


Create a novel class FeedItem.java

package com.blogspot.android_er.androidparsejson;  populace class FeedItem {     someone String title;     someone String url;      FeedItem(String title, String url){         this.title = title;         this.url = url;     }      populace String getTitle(){         provide title;     }      populace String getUrl(){         provide url;     }      @Override     populace String toString() {         provide championship + " :\n" + url;     } }  

MainActivity.java

package com.blogspot.android_er.androidparsejson;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;  import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List;  populace class MainActivity extends AppCompatActivity {      Button btnLoadFeed;     TextView textViewFeedUrl;     ListView listViewFeed;      List<FeedItem> listFeedItems;     ListAdapter adapterFeed;      String myFeed = "https://rootandroid.org//feeds/posts/default?alt=json";     //String myFeed = "http://arduino-er.blogspot.com/feeds/posts/default?alt=json";     //String myFeed = "http://helloraspberrypi.blogspot.com/feeds/posts/default?alt=json";     //String myFeed = "http://photo-er.blogspot.com/feeds/posts/default?alt=json";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnLoadFeed = (Button)findViewById(R.id.loadfeed);         textViewFeedUrl = (TextView)findViewById(R.id.feedurl);         listViewFeed = (ListView)findViewById(R.id.listviewfeed);          listFeedItems = novel ArrayList<>();         adapterFeed = novel ArrayAdapter<FeedItem>(                 this, android.R.layout.simple_list_item_1, listFeedItems);         listViewFeed.setAdapter(adapterFeed);          btnLoadFeed.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 textViewFeedUrl.setText(myFeed);                 novel JsonTask(listFeedItems, listViewFeed).execute(myFeed);             }         });     }      /*     JsonTask:     AsyncTask to download together with parse JSON Feed of blogspot inwards background      */     someone class JsonTask extends AsyncTask<String, FeedItem, String> {          List<FeedItem> jsonTaskList;         ListView jsonTaskListView;          populace JsonTask(List<FeedItem> targetList, ListView targetListView) {             super();             jsonTaskList = targetList;             jsonTaskListView = targetListView;         }          @Override         protected void onPreExecute() {             super.onPreExecute();             jsonTaskList.clear();             jsonTaskListView.invalidateViews();         }          @Override         protected String doInBackground(String... params) {              endeavor {                 terminal String queryResult = sendQuery(params[0]);                 parseQueryResult(queryResult);             } choose grip of (IOException e) {                 e.printStackTrace();                  terminal String eString = e.toString();                 runOnUiThread(new Runnable() {                     @Override                     populace void run() {                         Toast.makeText(MainActivity.this,                                 eString,                                 Toast.LENGTH_LONG).show();                     }                 });              } choose grip of (JSONException e) {                 e.printStackTrace();                  terminal String eString = e.toString();                 runOnUiThread(new Runnable() {                     @Override                     populace void run() {                         Toast.makeText(MainActivity.this,                                 eString,                                 Toast.LENGTH_LONG).show();                     }                 });             }             provide null;         }          @Override         protected void onProgressUpdate(FeedItem... values) {             FeedItem newItem = values[0];             jsonTaskList.add(newItem);             jsonTaskListView.invalidateViews();         }          someone String sendQuery(String query) throws IOException {             String queryReturn = "";             URL queryURL = novel URL(query);              HttpURLConnection httpURLConnection = (HttpURLConnection)queryURL.openConnection();              if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){                 InputStreamReader inputStreamReader =                         novel InputStreamReader(httpURLConnection.getInputStream());                 BufferedReader bufferedReader = novel BufferedReader(                         inputStreamReader, 8192);                 String business = null;                 while((line = bufferedReader.readLine()) != null){                     queryReturn += line;                 }                  bufferedReader.close();             }               provide queryReturn;         }          someone void parseQueryResult(String json) throws JSONException {             JSONObject jsonObject = novel JSONObject(json);             terminal JSONObject jsonObject_feed = jsonObject.getJSONObject("feed");             terminal JSONArray jsonArray_entry = jsonObject_feed.getJSONArray("entry");              runOnUiThread(new Runnable() {                 @Override                 populace void run() {                     if(jsonArray_entry == null){                         Toast.makeText(MainActivity.this,                                 "jsonArray_entry == null",                                 Toast.LENGTH_LONG).show();                     }else{                         Toast.makeText(MainActivity.this,                                 String.valueOf(jsonArray_entry.length()),                                 Toast.LENGTH_LONG).show();                         for(int i=0; i<jsonArray_entry.length(); i++){                             endeavor {                                 JSONObject thisEntry = (JSONObject) jsonArray_entry.get(i);                                 JSONObject thisEntryTitle = thisEntry.getJSONObject("title");                                 String thisEntryTitleString = thisEntryTitle.getString("$t");                                  JSONArray jsonArray_EntryLink = thisEntry.getJSONArray("link");                                  //search for the link chemical component alongside rel="alternate"                                 //I assume it's i together with alone i chemical component alongside rel="alternate",                                 //and its href concord the link to the page                                 for(int j=0; j<jsonArray_EntryLink.length(); j++){                                     JSONObject thisLink = (JSONObject) jsonArray_EntryLink.get(j);                                     try{                                         String thisLinkRel = thisLink.getString("rel");                                         if(thisLinkRel.equals("alternate")){                                             try{                                                 String thisLinkHref = thisLink.getString("href");                                                 FeedItem thisElement = novel FeedItem(                                                         thisEntryTitleString.toString(),                                                         thisLinkHref);                                                 publishProgress(thisElement);                                                 break;                                             }catch (JSONException e){                                                 //no such mapping exists                                             }                                         }                                     }catch (JSONException e){                                         //no such mapping exists                                     }                                  }                                                              } choose grip of (JSONException e) {                                 e.printStackTrace();                             }                          }                     }                  }             });         }     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidparsejson.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/loadfeed"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Load Feed"/>     <TextView         android:id="@+id/feedurl"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold|italic"/>     <ListView         android:id="@+id/listviewfeed"         android:layout_width="match_parent"         android:layout_height="match_parent"/> </LinearLayout>  

uses-permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml.

 I download together with parse the json feed inwards AsyncTask Download together with parse blogspot JSON feedDownload the files .

Next:
– Parse blogspot JSON feed: notice detail clicked to opened upwardly inwards browser using CustomTabsIntent


Remark@2017-05-20:
This video present how I TRY to locate the thumbnail from the JSON feed. In company to disclose the url of the thumbnail of the posts, you lot convey to search “media$thumbnail” elements inwards the feeds. But I haven’t truly implemented the code.