类型为org.json.JSONObject $ 1的值null无法转换为JSONArray

时间:2014-08-26 06:17:33

标签: php android json listview

这是我的列表查看活动代码 列表视图:

private static String url = "http://xxx.x.x.xxx/index.php";
String categ = null;
private static final String name = "name";
private static final String Category = "Category";

ListView lv;

String returnString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_listview);

    Bundle b = getIntent().getExtras();
    categ = b.getString("category_name");

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

}


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));
        }

    }

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        context = activity;
    }

    private Context context;

    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        //Toast.makeText(getApplicationContext(), url, 0).show();
        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) {

                @SuppressWarnings("unchecked")
                HashMap<String, String> map = (HashMap<String, String>) lv
                        .getItemAtPosition(position);
                String name = map.get("name");
                Intent intent = new Intent(ListDataActivity.this,
                        DataViewActivity.class);
                intent.putExtra("itemName", name);
                startActivity(intent);
            }
        });
    }

    protected Boolean doInBackground(final String... args) {
        url = url + "?cat_name=" + categ;
        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 to download file");
        }
    } 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;

}

我的问题是我将categ从android传递到php并在列表视图中显示数据工作正常但是当我将新数据传递给此活动时问题被反击,它在listview中显示相同的结果我从之前的活动传递的第一个数据..

0 个答案:

没有答案