Updated ReportListFragment.java to use a Loader to retrieve the data from the internet.
Created JSONLoader.java to handle downloading and parsing of a JSON Object from the internet.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 4.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.8 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.6 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 6.1 KiB |
Binary file not shown.
@@ -191,6 +191,7 @@ or to a theme attribute in the form "<code>?[<i>package</i>:][<i>type</i>:]<i>na
|
|||||||
public static final int send=0x7f040003;
|
public static final int send=0x7f040003;
|
||||||
public static final int sending=0x7f040002;
|
public static final int sending=0x7f040002;
|
||||||
public static final int sorry=0x7f040001;
|
public static final int sorry=0x7f040001;
|
||||||
|
public static final int there_was_an_error=0x7f040008;
|
||||||
public static final int version=0x7f040005;
|
public static final int version=0x7f040005;
|
||||||
}
|
}
|
||||||
public static final class style {
|
public static final class style {
|
||||||
|
|||||||
@@ -191,6 +191,7 @@ or to a theme attribute in the form "<code>?[<i>package</i>:][<i>type</i>:]<i>na
|
|||||||
public static final int send=0x7f040003;
|
public static final int send=0x7f040003;
|
||||||
public static final int sending=0x7f040002;
|
public static final int sending=0x7f040002;
|
||||||
public static final int sorry=0x7f040001;
|
public static final int sorry=0x7f040001;
|
||||||
|
public static final int there_was_an_error=0x7f040008;
|
||||||
public static final int version=0x7f040005;
|
public static final int version=0x7f040005;
|
||||||
}
|
}
|
||||||
public static final class style {
|
public static final class style {
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
|
|
||||||
<string name="hello">Hello World, Main!</string>
|
<string name="hello">Hello World, Main!</string>
|
||||||
<string name="app_name">ExceptionReportViewer</string>
|
<string name="app_name">ExceptionReportViewer</string>
|
||||||
|
<string name="there_was_an_error">There was an error...</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
/**
|
||||||
|
* JSONLoader.java
|
||||||
|
* @date Dec 28, 2011
|
||||||
|
* @author ricky barrette
|
||||||
|
* @author Twenty Codes, LLC
|
||||||
|
*/
|
||||||
|
package com.TwentyCodes.android.ExceptionReportViewer;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
import org.apache.http.client.ClientProtocolException;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v4.app.LoaderManager;
|
||||||
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
||||||
|
import android.support.v4.content.Loader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Loader will be used to download and parse a JSON object from the internet
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class JSONLoader extends Loader<JSONObject> {
|
||||||
|
|
||||||
|
private LoaderCallbacks<JSONObject> mListener;
|
||||||
|
private String mUrl;
|
||||||
|
private InputStream mContent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an new JSONLoader
|
||||||
|
* @param url
|
||||||
|
* @param listener
|
||||||
|
* @param context
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public JSONLoader(String url, LoaderManager.LoaderCallbacks<JSONObject> listener, Context context) {
|
||||||
|
super(context);
|
||||||
|
mListener = listener;
|
||||||
|
mUrl = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.content.Loader#onAbandon()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onAbandon() {
|
||||||
|
super.onAbandon();
|
||||||
|
if(mContent != null)
|
||||||
|
try {
|
||||||
|
mContent.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.content.Loader#onForceLoad()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onForceLoad() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
super.onForceLoad();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.content.Loader#onReset()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onReset() {
|
||||||
|
super.onReset();
|
||||||
|
if(mContent != null)
|
||||||
|
try {
|
||||||
|
mContent.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(mListener != null)
|
||||||
|
mListener.onLoaderReset(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.content.Loader#onStartLoading()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onStartLoading() {
|
||||||
|
super.onStartLoading();
|
||||||
|
if(mListener != null)
|
||||||
|
try {
|
||||||
|
mListener.onLoadFinished(this, new JSONObject(downloadJSON(mUrl)));
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
mListener.onLoadFinished(this, null);
|
||||||
|
} catch (ClientProtocolException e) {
|
||||||
|
mListener.onLoadFinished(this, null);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
mListener.onLoadFinished(this, null);
|
||||||
|
} catch (IOException e) {
|
||||||
|
mListener.onLoadFinished(this, null);
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
mListener.onLoadFinished(this, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.content.Loader#onStopLoading()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void onStopLoading() {
|
||||||
|
super.onStopLoading();
|
||||||
|
if(mContent != null)
|
||||||
|
try {
|
||||||
|
mContent.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads exception report JSON from the Internet
|
||||||
|
* @param url
|
||||||
|
* @return
|
||||||
|
* @throws IllegalStateException
|
||||||
|
* @throws ClientProtocolException
|
||||||
|
* @throws IOException
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
private String downloadJSON(String url) throws IllegalStateException, ClientProtocolException, IOException {
|
||||||
|
if(url == null)
|
||||||
|
throw new NullPointerException();
|
||||||
|
|
||||||
|
StringBuffer response = new StringBuffer();
|
||||||
|
mContent = new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent();
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(mContent));
|
||||||
|
String buff = null;
|
||||||
|
while ((buff = br.readLine()) != null){
|
||||||
|
System.out.print(buff);
|
||||||
|
response.append(buff);
|
||||||
|
}
|
||||||
|
return response.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,37 +6,30 @@
|
|||||||
*/
|
*/
|
||||||
package com.TwentyCodes.android.ExceptionReportViewer;
|
package com.TwentyCodes.android.ExceptionReportViewer;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import com.TwentyCodes.android.exception.ExceptionReportActivity;
|
|
||||||
import com.TwentyCodes.android.exception.Report;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Handler;
|
import android.os.Bundle;
|
||||||
import android.os.Message;
|
|
||||||
import android.support.v4.app.ListFragment;
|
import android.support.v4.app.ListFragment;
|
||||||
|
import android.support.v4.app.LoaderManager;
|
||||||
|
import android.support.v4.content.Loader;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.TwentyCodes.android.exception.ExceptionReportActivity;
|
||||||
|
import com.TwentyCodes.android.exception.Report;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This fragment will be used to display a list of exception reports to the user
|
* This fragment will be used to display a list of exception reports to the user
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public class ReportListFragment extends ListFragment {
|
public class ReportListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<JSONObject> {
|
||||||
|
|
||||||
protected static final int DOWNLOADED_REPORTS = 0;
|
|
||||||
protected static final int ERROR = 1;
|
|
||||||
private JSONArray mReports;
|
private JSONArray mReports;
|
||||||
|
private String mUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new ReportListFragment
|
* Creates a new ReportListFragment
|
||||||
@@ -44,91 +37,19 @@ public class ReportListFragment extends ListFragment {
|
|||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public ReportListFragment(final String url) {
|
public ReportListFragment(final String url) {
|
||||||
|
mUrl = url;
|
||||||
final Handler handler = new Handler(){
|
|
||||||
|
|
||||||
/**
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see android.os.Handler#handleMessage(android.os.Message)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
switch(msg.what){
|
|
||||||
case DOWNLOADED_REPORTS:
|
|
||||||
try {
|
|
||||||
parseJSON((String) msg.obj);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
ReportListFragment.this.setEmptyText(e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ERROR:
|
|
||||||
ReportListFragment.this.setEmptyText((String) msg.obj);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
super.handleMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
new Thread(new Runnable(){
|
|
||||||
@Override
|
|
||||||
public void run(){
|
|
||||||
/*
|
|
||||||
* Here we will try to download and parse the reports from the server
|
|
||||||
* if there is any errors, the user is notified via the list's empty text view
|
|
||||||
*/
|
|
||||||
try {
|
|
||||||
handler.sendMessage(handler.obtainMessage(DOWNLOADED_REPORTS, downloadJSON(url)));
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage()));
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (ClientProtocolException e) {
|
|
||||||
handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage()));
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
handler.sendMessage(handler.obtainMessage(ERROR, e.getMessage()));
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parses the JSON reports and displays them in a list
|
* (non-Javadoc)
|
||||||
* @param json
|
* @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
|
||||||
* @throws JSONException
|
|
||||||
* @author ricky barrette
|
|
||||||
*/
|
*/
|
||||||
private void parseJSON(String json) throws JSONException {
|
@Override
|
||||||
mReports = new JSONObject(json).getJSONArray("reports");
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
this.setListAdapter(new ReportAdapter(this.getActivity(), mReports));
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
getLoaderManager().initLoader(0, null, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Downloads exception report JSON from the Internet
|
|
||||||
* @param url
|
|
||||||
* @return
|
|
||||||
* @throws IllegalStateException
|
|
||||||
* @throws ClientProtocolException
|
|
||||||
* @throws IOException
|
|
||||||
* @author ricky barrette
|
|
||||||
*/
|
|
||||||
private String downloadJSON(String url) throws IllegalStateException, ClientProtocolException, IOException {
|
|
||||||
if(url == null)
|
|
||||||
throw new NullPointerException();
|
|
||||||
StringBuffer response = new StringBuffer();
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(new DefaultHttpClient().execute(new HttpGet(url)).getEntity().getContent()));
|
|
||||||
String buff = null;
|
|
||||||
while ((buff = br.readLine()) != null){
|
|
||||||
System.out.print(buff);
|
|
||||||
response.append(buff);
|
|
||||||
}
|
|
||||||
return response.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the user selects a report to display
|
* Called when the user selects a report to display
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
@@ -144,4 +65,38 @@ public class ReportListFragment extends ListFragment {
|
|||||||
Toast.makeText(this.getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
|
Toast.makeText(this.getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the loader is created
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.app.LoaderManager.LoaderCallbacks#onCreateLoader(int, android.os.Bundle)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Loader<JSONObject> onCreateLoader(int id, Bundle args) {
|
||||||
|
return new JSONLoader(mUrl, this, this.getActivity());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the loader has finished
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.support.v4.content.Loader, java.lang.Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onLoadFinished(Loader<JSONObject> loader, JSONObject jsonObject) {
|
||||||
|
if(jsonObject != null){
|
||||||
|
try {
|
||||||
|
mReports = jsonObject.getJSONArray("reports");
|
||||||
|
this.setListAdapter(new ReportAdapter(this.getActivity(), mReports));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
this.setEmptyText(getText(R.string.there_was_an_error));
|
||||||
|
}
|
||||||
|
this.setEmptyText(getText(R.string.there_was_an_error));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoaderReset(Loader<JSONObject> loader) {
|
||||||
|
//not needed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user