JSONArray null内容Java& Android的

时间:2012-07-14 17:02:12

标签: java android arrays json object

有可能在ArrayList中不可能放大内容然后如果你这样做会给你null?

我正在插入一个带有32个JSONObject的JSONArray,每个JSONObject都有姓名和电话联系。

ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("something", content));


public void phoneandname(Context c) {

    if (cc.isOnline(c) == true) {
        db = new Database(c);

        Cursor phones = c.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);

        JSONArray jarray = new JSONArray();
        db.open();

        while (phones.moveToNext()) {
            String name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            db.contact_table(name, phoneNumber);

            JSONObject json = new JSONObject();

            try {

                json.put("name", name);
                json.put("phone", phoneNumber);
                jarray.put(json);

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

        }

        upload(jarray, context);

        db.close();

        Log.i("********", jarray.toString()); IT SHOWS MY ARRAY AND THEN BEFORE UPLOAD IT TO THE SERVER BY POST IT'S NULL...
        phones.close();
    }

}

public void upload(Object jobject, Context c) {

    ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
    DBupload upload = new DBupload();
    DatosRegistro dr = new DatosRegistro(c);

    nvp.add(new BasicNameValuePair("content", jobject.toString()));
    nvp.add(new BasicNameValuePair("token", dr.hash_final(
            dr.token_secret(dr.mail(), "mail"), jobject.toString())));
    nvp.add(new BasicNameValuePair("id", String.valueOf(dr
            .return_id_phone())));

    JSONObject json2 = upload.UploaData(nvp,
            "http://anonyme.mariomontes.es/contacts/insert");

    try {
        if (json2.getInt("error") == 1) {

        } else {
            db.contact_table(name, phoneNumber);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

ArrayList<NameValuePair> nvp = new ArraList<BasicNameValuePair>(); 
BasicNameValuePair tmp = new BasicNameValuePair("something", content);
nvp.add(tmp); 

您有ArrayList NameValuePair。因此,您需要在NameValuePair

中放置一个ArrayList对象

答案 1 :(得分:0)

据我所知,唯一限制就是记忆。

我有一些巨大的JSONArray实例,里面有更大的对象。

我不确定您的代码示例与之相关。

我通常会这样做:

JSONArray ja = new JSONArray();

//multiples of these
JSONObject jo = new JSONObject();
jo.put("name","Marc");
jo.put("phone","1234567890");

ja.put(jo);

<强>更新

好的,我明白你的意思了......完全不同的问题。

所以问题更像是你在说这个:

nvp.add(new BasicNameValuePair("content", jobject.toString()));

但是您将JSONArray作为对象传递...您需要将其转换回JSONArray

nvp.add(new BasicNameValuePair("content",((JSONArray)jobject).toString()));

更好的方法是将字符串传递给上传函数而不是作为对象