Parse Blogspot Json Feed: Notice Detail Clicked To Opened Upwards Inwards Browser Using Customtabsintent

Posted on

Last post service demonstrate a representative to “Download as well as parse blogspot JSON feed“. In this post, OnItemClickListener is added to the ListView; for user clicking to opened upwards the url inwards browser, using “Simplest means to opened upwards browser using CustomTabsIntent.Builder“.


To use CustomTabsIntent.Builder() inwards your project, y’all convey to edit appbuild.gradle to add together dependencies of compile ‘com.android.support:customtabs:23.0.0’.

dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     testCompile 'junit:junit:4.12'     compile 'com.android.support:appcompat-v7:23.4.0'     compile 'com.android.support:customtabs:23.0.0' } 

Edit MainActivity.java to add OnItemClickListener.

package com.blogspot.android_er.androidparsejson;  import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.customtabs.CustomTabsIntent; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; 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 shape 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);         listViewFeed.setOnItemClickListener(listViewFeedOnItemClickListener);          btnLoadFeed.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 textViewFeedUrl.setText(myFeed);                 novel JsonTask(listFeedItems, listViewFeed).execute(myFeed);             }         });     }      AdapterView.OnItemClickListener listViewFeedOnItemClickListener =             novel AdapterView.OnItemClickListener(){                 @Override                 populace void onItemClick(AdapterView<?> parent, View view, int position, long id) {                     FeedItem clickedFeedItem = (FeedItem) parent.getItemAtPosition(position);                     String url = clickedFeedItem.getUrl();                     Uri uri = Uri.parse(url);                     novel CustomTabsIntent.Builder()                             .build()                             .launchUrl(MainActivity.this, uri);                 }             };      /*     JsonTask:     AsyncTask to download as well as parse JSON Feed of blogspot inwards background      */     somebody shape 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) {              crusade {                 concluding String queryResult = sendQuery(params[0]);                 parseQueryResult(queryResult);             } grab (IOException e) {                 e.printStackTrace();                  concluding String eString = e.toString();                 runOnUiThread(new Runnable() {                     @Override                     populace void run() {                         Toast.makeText(MainActivity.this,                                 eString,                                 Toast.LENGTH_LONG).show();                     }                 });              } grab (JSONException e) {                 e.printStackTrace();                  concluding String eString = e.toString();                 runOnUiThread(new Runnable() {                     @Override                     populace void run() {                         Toast.makeText(MainActivity.this,                                 eString,                                 Toast.LENGTH_LONG).show();                     }                 });             }             supply null;         }          @Override         protected void onProgressUpdate(FeedItem... values) {             FeedItem newItem = values[0];             jsonTaskList.add(newItem);             jsonTaskListView.invalidateViews();         }          somebody 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();             }               supply queryReturn;         }          somebody void parseQueryResult(String json) throws JSONException {             JSONObject jsonObject = novel JSONObject(json);             concluding JSONObject jsonObject_feed = jsonObject.getJSONObject("feed");             concluding 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++){                             crusade {                                 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 cistron alongside rel="alternate"                                 //I assume it's i as well as solely i chemical cistron alongside rel="alternate",                                 //and its href concur 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                                     }                                  }                              } grab (JSONException e) {                                 e.printStackTrace();                             }                          }                     }                  }             });         }     } }  

Other files, FeedItem.java, activity_main.xml and AndroidManifest.xml, scream last post.