【问题标题】:JSONArray null content Java & AndroidJSONArray 空内容 Java & Android
【发布时间】:2012-07-14 04:49:21
【问题描述】:

有可能在 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();
    }
}

谢谢!

【问题讨论】:

  • 您还可以编辑实际问题吗?代码看起来不错:)

标签: java android arrays json object


【解决方案1】:
ArrayList<NameValuePair> nvp = new ArraList<BasicNameValuePair>(); 
BasicNameValuePair tmp = new BasicNameValuePair("something", content);
nvp.add(tmp); 

您有一个 ArrayListNameValuePair。所以你需要在ArrayList 里面放一个NameValuePair 对象

【讨论】:

  • 当我将它上传到服务器时,内容请求它是空的......也许这是一个大小问题? :)
【解决方案2】:

据我所知,唯一的限制是内存。

我有一些巨大的 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()));

最好将字符串传递给您的上传函数而不是作为对象

【讨论】:

  • 仍然无法正常工作...服务器说请愿书是 content=null :(
  • 那么你现在是通过字符串传递的吗?如果是这样,请设置一个断点并在发送之前验证您的字符串是否包含您认为它包含的内容。
  • ... 然后在消息到达服务器时使用 fiddler2 检查消息
  • 是的,如果您查看代码,上传后我正在执行 Log.i();与内容,它完美地展示了我想要的。
  • 尝试在上传功能中执行此操作以进行完整性检查。您下载了 fiddler2 并查看消息中的内容吗?
猜你喜欢
  • 2021-07-15
  • 2021-12-03
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
相关资源
最近更新 更多