Back to project page blog-reader.
The source code is released under:
GNU General Public License
If you think the Android project blog-reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ca.exilium.blogreader; //w w w. ja v a2 s . c o m import android.app.AlertDialog; import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class MainListActivity extends ListActivity { public static final int NUMBER_OF_POSTS = 25; public static final String TAG = MainListActivity.class.getSimpleName(); private final String KEY_TITLE = "title"; private final String KEY_AUTHOR = "author"; protected JSONObject _blogData; protected ProgressBar _progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_list); _progressBar = (ProgressBar) findViewById(R.id.progressBar); if(networkAvailable()) { _progressBar.setVisibility(View.VISIBLE); GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); getBlogPostsTask.execute(); } else { Toast.makeText(this, "Network is not available", Toast.LENGTH_LONG).show(); } } @Override protected void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); try { JSONArray jsonPosts = _blogData.getJSONArray("posts"); JSONObject jsonPost = jsonPosts.getJSONObject(position); String blogUrl = jsonPost.getString("url"); Intent intent = new Intent(this, BlogWebViewActivity.class); intent.setData(Uri.parse(blogUrl)); startActivity(intent); } catch (JSONException e) { logException(e); } } private void logException(Exception e) { Log.e(TAG, "Exception caught:", e); } private boolean networkAvailable() { ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); } private void handleBlogResponse() { _progressBar.setVisibility(View.INVISIBLE); if(_blogData == null) { updateDisplayForError(); } else { try { JSONArray jsonPosts = _blogData.getJSONArray("posts"); ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>(); for(int i = 0; i < jsonPosts.length(); i++) { JSONObject post = jsonPosts.getJSONObject(i); String title = post.getString(KEY_TITLE); title = Html.fromHtml(title).toString(); String author = post.getString(KEY_AUTHOR); author = Html.fromHtml(title).toString(); HashMap<String, String> blogPost = new HashMap<String, String>(); blogPost.put(KEY_TITLE, title); blogPost.put(KEY_AUTHOR, author); blogPosts.add(blogPost); } String[] keys = { KEY_TITLE, KEY_AUTHOR }; int[] ids = { android.R.id.text1, android.R.id.text2 }; SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_expandable_list_item_2, keys, ids); setListAdapter(adapter); } catch (JSONException e) { logException(e); } } } private void updateDisplayForError() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.error_title)); builder.setMessage(getString(R.string.error_message)); builder.setPositiveButton(android.R.string.ok, null); AlertDialog dialog = builder.create(); dialog.show(); TextView emptyTextView = (TextView) getListView().getEmptyView(); emptyTextView.setText(getString(R.string.no_items)); } private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> { @Override protected JSONObject doInBackground(Object[] params) { int responseCode; JSONObject jsonResponse = null; try { URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); responseCode = connection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); int contentLength = connection.getContentLength(); char[] charArray = new char[contentLength]; reader.read(charArray); String responseData = new String(charArray); jsonResponse = new JSONObject(responseData); } else { Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode); } } catch (MalformedURLException e) { logException(e); } catch (IOException e) { logException(e); } catch (Exception e) { logException(e); } return jsonResponse; } @Override protected void onPostExecute(JSONObject result) { _blogData = result; handleBlogResponse(); } } }