解析数据时出错org.json.JSONException:org.json.JSONArray类型的Value []无法转换为JSONObject

时间:2016-07-20 12:01:00

标签: android json

我仍然是新的android和json请帮助我我也得到Null Pointer Exception。我该怎么办?在此先感谢

继承我的代码:

JSONArray ass=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ass_list);
    new JSONParse().execute();
}

//JSON
private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {

    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl("http://192.168.43.252/mobappproj/asslist.php");
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        final ArrayList<String> ids = new ArrayList<String>();
        final ArrayList<String> assname = new ArrayList<String>();
        final ArrayList<String> duedate = new ArrayList<String>();
        final ArrayList<String> course = new ArrayList<String>();
        final ArrayList<String> note = new ArrayList<String>();
        try {
            ass = json.getJSONArray("ass");
            for(int i=0; i<ass.length(); i++){
                JSONObject student = ass.getJSONObject(i);
                ids.add(student.getString("id"));
                assname.add(student.getString("assname"));
                duedate.add(student.getString("duedate"));
                course.add(student.getString("course"));
                note.add(student.getString("note"));

            }
            final ListView listView = (ListView)findViewById(R.id.listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AssList.this,android.R.layout.simple_list_item_1, android.R.id.text1, course);
            // Assign adapter to ListView
            listView.setAdapter(adapter);
            // ListView Item Click Listener
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // ListView Clicked item value                       
                    String ass_id = ids.get(position);
                    String ass_name = assname.get(position);
                    String ass_date = duedate.get(position);
                    String ass_course = course.get(position);
                    String ass_note = note.get(position);
                    Intent i = new Intent(AssList.this, ViewAss.class);
                    i.putExtra("ass_id", ass_id);
                    i.putExtra("ass_name", ass_name);
                    i.putExtra("ass_date", ass_date);
                    i.putExtra("ass_course", ass_course);
                    i.putExtra("ass_note", ass_note);
                    startActivity(i);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

这是我得到的错误:

07-20 19:20:35.635 30660-30711/com.example.shaishai.assignmenttrackertry E/JSON Parser: Error parsing data org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

0 个答案:

没有答案