2012-05-26 52 views
1

我試圖JSON數組加載到Viewlist產品, 但我不能加載應用程序,因爲運算錯誤一,的Json和ListView在Android的崩潰

JSONParser.java

package app.tabsample; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 

public class JSONParser extends ListActivity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contactspage); 

     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.personallist())); 
    } 

    public ArrayList<String> personallist() { 

     ArrayList<String> listItems = new ArrayList<String>(); 

     try { 

      URL jsonurl = new URL("http://izra.co.il/z.php"); 



      URLConnection transport = jsonurl.openConnection(); 


      BufferedReader in = new BufferedReader(new InputStreamReader(
        transport.getInputStream())); 


      String line; 



      while ((line = in.readLine()) != null) { 
       JSONObject obj = new JSONObject(line); 
       JSONArray ja = obj.getJSONArray("users"); 
       for (int i = 0; i < ja.length(); i++) { 
        JSONObject jo = (JSONObject) ja.get(i); 
        listItems.add(jo.getString("name")); 
        Log.v("got name",jo.getString("name")); 
       } 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace();// Catch URL Error 
     } catch (IOException e) { 
      e.printStackTrace();// Catch IO Exception Error 
     } catch (JSONException e) { 
      e.printStackTrace(); // Catch JSON Exception Error 
     } 
     return listItems; 
    } 
} 

contactpage。 XML

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 


</LinearLayout> 

一些如何stucks在這些線路:

 URLConnection transport = jsonurl.openConnection(); 

     BufferedReader in = new BufferedReader(new InputStreamReader(
       transport.getInputStream())); 

錯誤:

05-26 15:39:08.902: E/AndroidRuntime(2931): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.tabsample/app.tabsample.JSONParser}: android.os.NetworkOnMainThreadException 

希望有人能幫助我,我疲憊不堪......

非常感謝。

回答