Android Parse JSON String for Listview within View Flipper
I'm just trying to parse a simple JSON string in the following format: ["X","Y","Z"] and put it into a listview. The listview is within a view flipper in order to have tabs visible at all times throughout the app.
The problem i'm having is the app is stopping unexpectedly, here is the full code for the main launching activity:
public class Activity extends TabActivity implements OnClickListener {
Button doSomething;
TabHost tabHost;
ViewFlipper flipper;
ListView listview;
HttpResponse re;
String json;
JSONObject j;
@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout_1);
doSomething = (Button) findViewById(R.id.btn_do_something);
doSomething.setOnClickListener(this);
flipper = (ViewFlipper) findViewById(R.id.layout_tab_one);
listview = (ListView) findViewById(R.id.listview);
//@SuppressWarnings("unchecked")
ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetch());
listview.setAdapter(adapter);
flipper.setOnClickListener(this);
String tabname1 = getString(R.string.tabexample_tab1);
String tabname2 = getString(R.string.tabexample_tab2);
String tabname3 = getString(R.string.tabexample_tab3);
String tabname4 = getString(R.string.tabexample_tab4);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1));
tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2));
tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3));
tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4));
tabHost.setCurrentTab(0);
listview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
flipper.showNext();
}});
}
public ArrayList<String> fetch()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"JSON.php");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
//make sure youe String line is completely filled after that..
if (!line.equals(null) && !line.equals("") && line.startsWith("["))
{
JSONArray jArray = new JSONArray(line);
for (int i = 0; i < jArray.length(); i++)
{
JSONObject jobj = jArray.getJSONObject(i);
// also make sure you get the value from the jsonObject using some key
// like, jobj.getString("country");
listItems.add(jobj.getString(""));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Log output:
08-25 19:29:48.544: DEBUG/AndroidRuntime(850): Shutting down VM
08-25 19:29:48.544: WARN/dalvikvm(850): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): FATAL EXCEPTION: main
08-25 19:29:48.564: ERROR/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.android/com.app.android.Activity}: android.os.NetworkOnMainThreadException
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.os.Looper.loop(Looper.java:132)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at java.lang.reflect.Method.invoke(Method.java:491)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at dalvik.system.NativeStart.main(Native Method)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): Caused by: android.os.NetworkOnMainThreadException
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at java.net.InetAddress.getAllByName(InetAddress.java:249)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at com.android.Activity.fetch(Activity.java:193)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at com.android.Activity.onCreate(Activity.java:144)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.Activity.performCreate(Activity.java:4397)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): ... 11 more
Ok the app is launching no开发者_JS百科w but the listview is not being populated with the JSON Strings for some reason?
Your app is throwing a NetworkOnMainThreadException
, and the documentation says this about it:
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.
Looking at your code, you call fetch()
in your onCreate()
, which runs on the UI Thread. You will need to do this in a background thread (or target an SDK version less than Honeycomb). Take a look at AsyncTask
for some more info on this solution.
ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetch());
精彩评论