2014-09-06 144 views
-1

以下是列表活动。解析数据时出错org.json.JSONException:值字符串不能转换为JSONArray

列表活动:

public class ListDataActivity extends ListActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_listview); 

    url = "http://xxx.xx.x.xxx/index.php"; 

    Bundle c = getIntent().getExtras(); 
    blo = c.getString("blood"); 

    new ProgressTask(ListDataActivity.this).execute(); 

} 

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

} 

class ProgressTask extends AsyncTask<String, Void, Boolean> { 

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>(); 

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    { 
     if (conMgr.getActiveNetworkInfo() != null 
       && conMgr.getActiveNetworkInfo().isAvailable() 
       && conMgr.getActiveNetworkInfo().isConnected()) { 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "INTERNET CONNECTION NOT PRESENT", Toast.LENGTH_SHORT) 
        .show(); 
      startActivity(new Intent(ListDataActivity.this, 
        MainActivity.class)); 
     } 

    } 


    public ProgressTask(ListActivity activity) { 
     context = activity; 
    } 

    private Context context; 

    protected void onPreExecute() { 

    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 
    ListAdapter adapter = new SimpleAdapter(context, jsonlist, 
       R.layout.row_listitem, new String[] { name, Category }, 
       new int[] { R.id.vehicleType, R.id.vehicleColor }) { 

      @Override 
      public View getView(int position, View convertView, 
        ViewGroup parent) { 

       if (convertView == null) { 

        // This a new view we inflate the new layout 

        LayoutInflater inflater = (LayoutInflater) context 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        convertView = inflater.inflate(R.layout.row_listitem, 
          null); 
       } 

       // TODO Auto-generated method stub 
       if (position % 2 == 1) { 

        convertView.setBackgroundColor(Color.rgb(120, 151, 66)); 
       } else { 
        convertView.setBackgroundColor(Color.rgb(86, 107, 129)); 
       } 
       return super.getView(position, convertView, parent); 
      } 
     }; 

     setListAdapter(adapter); 

     lv = getListView(); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
      } 
     }); 
    } 

    protected Boolean doInBackground(final String... args) { 
      url = url + "?blo_name=" + blo; 
     Log.d("", url); 

     baseAdapter jParser = new baseAdapter(); 

     JSONArray json = jParser.getJSONFromUrl(url); 

     if (json != null) { 
      for (int i = 0; i < json.length(); i++) { 

       try { 
        JSONObject c = json.getJSONObject(i); 

        String vtype = c.getString(name); 
        String vfuel = c.getString(Category); 

        HashMap<String, String> map = new HashMap<String, String>(); 

        // Add child node to HashMap key & value 
        map.put(name, vtype); 
        map.put(Category, vfuel); 

        jsonlist.add(map); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return null; 

    } 

底座适配器类:

public class baseAdapter { 

static InputStream iStream = null; 
static JSONArray jarray = null; 
static String json = ""; 

public baseAdapter() { 
} 

public JSONArray getJSONFromUrl(String url) { 

    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(url); 
    try { 
     HttpResponse response = client.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
     } else { 
      Log.e("==>", "Failed "); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // Parse String to JSON object 
    try { 

     jarray = new JSONArray(builder.toString()); 

    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON Object 
    return jarray; 

} 

PHP:

if(isset($_POST['blo_name'])){ 
$str = $_POST['blo_name']; 
$sql = "SELECT * FROM blood_group where Category IN ($str)"; 
$result = mysql_query($sql); 
while($row=mysql_fetch_array($result)) 
$output[]=$row; 
print(json_encode($output)); 
mysql_close(); 
} 

我的疑问句重刑是,我试图解析bol字符串,它有像bol="'B-','O-'";值,而我解析这种类型的值,它没有显示结果,并给出了logcat。

logcat的:

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray 

虽然我喜欢解析和"B,O"在DB的变化简单的价值它给我造成的罚款。但是,我需要解析这个价值。 请帮助我。谢谢

+0

的[错误解析数据org.json.JSONException:值
SilentKiller 2014-09-08 10:19:17

回答

0

正如你所说,你无法解析JSON字符串,就像“'B - ','O-'”,但你可以成功地解析字符串“B,O”。

这是由于单引号(')被认为是json的一个特殊字符,因此在解析它之前,您可能希望在字符串中像\'一样转义它。

更多细节请参考:How to escape special characters in building a JSON string?

0

我不知道php,但如果你使用Post Method制作API,你应该使用httpPost而不是httpGet