2013-04-09 70 views
0

我試圖用JSONArray解析字符串填充。我得到的logcat的警告:列表視圖不被JSON解析

org.json.JSON.typeMismatch(JSON.java:107) 
org.json.JSONArray.<init>(JSONArray.java:91) 
org.json.JSONArray.<init>(JSONArray.java:103) 

我的代碼:

@Override 
protected void onPostExecute(String result) { 
super.onPostExecute(result); 


mylist = new ArrayList<HashMap<String, String>>(); 
map = new HashMap<String, String>(); 
try { 

    JSONArray arr = new JSONArray(result); 

     for (int i = 0; i < arr.length(); i++) { 

      JSONObject jObj = arr.getJSONObject(i); 
      c_id = jObj.getString(CID); 
      j_make = jObj.getString(MAKE); 
      j_model = jObj.getString(MODEL); 
      j_version = jObj.getString(VERSION); 
      j_price = jObj.getString(PRICE); 
      j_reg_plc = jObj.getString(PLACE_REG); 

      data = "Make: " + j_make + "\nModel: " + j_model 
          + "\nVersion: " + j_version + "\nPrice: " + j_price 
        + "\nCity: " + j_reg_plc; 

      map.put("car", data); 
      mylist.add(map); 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    list = (ListView) findViewById(R.id.listView1); 

    String[] from = new String[] { "car" }; 
    int[] to = new int[] { R.id.text1 }; 
    ListAdapter adapter = new SimpleAdapter(view_cars.this, mylist, 
          R.layout.text_adaptr, from, to); 


} 

我無能WATS錯誤的,因爲同樣的代碼在不同的意圖工作。

----- UPDATE -----

我已經克服了這個警告。這是一個腳本錯誤。 但仍然代碼無法正常工作。

我收到JSON數組EG-[{ 「car_id」: 「22」, 「模型」: 「MODEL1」}]等

我的整個代碼

public class view_cars extends ListActivity { 

public String s_id, ID, is, str; 
String FILENAME = "http://animsinc.com/viewCars.php"; 

ListView list; 
String data; 

ArrayList<HashMap<String, String>> mylist; 

HashMap<String, String> map; 

static String j_id = null; 
static Object j_make = null; 
static String j_model = null; 
static String j_version = null; 
static String j_price = null; 
static String j_reg_plc = null; 

String CID = "car_id"; 
String MAKE = "make"; 
String MODEL = "model"; 
String VERSION = "version"; 
String PRICE = "expected_price"; 
String PLACE_REG = "registration_place"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_prof); 

    Bundle bundle = getIntent().getExtras(); 
    String stuff = bundle.getString("stuff"); 

    list = getListView(); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, values); 
     setListAdapter(adapter); 

    Toast.makeText(this, "try " + stuff, 1000).show(); 


    try { 

     JSONArray jArray = new JSONArray(stuff.toString()); 

     for (int i = 0; i < jArray.length(); i++) { 

      JSONObject jObject = jArray.getJSONObject(i); 
      s_id = jObject.getString("seller_id"); 
      // s_id = jObject.getString(ID); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    Toast.makeText(this, "JSON: " + s_id, 1000).show(); 

    startDownload(); 

} 


@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 

    Intent CheckProfile = new Intent(this, MenuPg.class); 
    startActivity(CheckProfile); 

} 

private void startDownload() { 

    new AppTask().execute(FILENAME); 
} 

public class AppTask extends AsyncTask<String, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     Toast.makeText(view_cars.this, ""+ result, 1000).show(); 
     // display(is); 

     // void display(String res){ 

     str = result; 

     mylist = new ArrayList<HashMap<String, String>>(); 
     map = new HashMap<String, String>(); 
     try { 
      Toast.makeText(getApplicationContext(),"Inside Try", Toast.LENGTH_LONG).show(); 
      JSONArray jJArray = new JSONArray(str); 

      for (int i = 0; i < jJArray.length(); i++) { 



       JSONArray e = jJArray.getJSONArray(i); 


       JSONObject jObject = jJArray.getJSONObject(i); 
       j_id = jObject.getString(ID); 

       j_make = jObject.getString(MAKE); 

       j_model = jObject.getString(MODEL); 

       j_version = jObject.getString(VERSION); 
       j_price = jObject.getString(PRICE); 
       j_reg_plc = jObject.getString(PLACE_REG); 

       data = "Make: "+ j_make + "\nModel: " + j_model + "\nVersion: " 
               + j_version + "\nPrice: " + j_price 
         + "\nCity: " + j_reg_plc; 

       Toast.makeText(getApplicationContext(),""+data, Toast.LENGTH_LONG).show(); 

       map.put("car", data); 

       mylist.add(map); 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 




    // list = (ListView) findViewById(R.id.listView1); 
     Toast.makeText(getApplicationContext(),""+mylist, Toast.LENGTH_LONG).show(); 
     String[] from = new String[] { "car"}; 
     int[] to = new int[] { R.id.text1 }; 
     ListAdapter adapt = new SimpleAdapter(view_cars.this, mylist, 
       R.layout.text_adaptr, from, to); 

     list.setAdapter(adapt); 

     // Assign adapter to ListView list.setAdapter(adapter); 




    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     // String is = null; 
     // str1 = et1.getText().toString(); 
     // str2 = et2.getText().toString(); 


     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(FILENAME); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        1); 
      nameValuePairs.add(new BasicNameValuePair("s_id", s_id)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 

      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = EntityUtils.toString(entity); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     return is; 
    } 
} 

} 
+0

發表您的全logcat的錯誤。 – rajeshwaran 2013-04-09 12:15:32

+0

可能是你的json返回的數據是jsonobject。 – rajeshwaran 2013-04-09 12:16:10

+0

我們可以在同一個意圖中執行兩次JSON解析嗎? – Carbon 2013-04-10 09:05:03

回答

1

嘗試實例它作爲一個JSONObject而不是你解析的數據不是一個數組而是一個對象。

JSONObject json = new JSONObject(result); 

以備將來參考,不同的時候使用權類型:JSONObject{}覆蓋和覆蓋[]JSONArray