2013-04-09 80 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