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

Actually I don’t know the format of blogspot json feed, I role Online JSON Viewer ( 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

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;     } }

package com.blogspot.android_er.androidparsejson;  import android.os.AsyncTask; import android.os.Bundle; import; 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; import; import; import; import; 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 = "";     //String myFeed = "";     //String myFeed = "";     //String myFeed = "";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnLoadFeed = (Button)findViewById(;         textViewFeedUrl = (TextView)findViewById(;         listViewFeed = (ListView)findViewById(;          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();                             }                          }                     }                  }             });         }     } }  


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""     xmlns: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=""         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 .

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

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.