【问题标题】:android http post with params带有参数的android http帖子
【发布时间】:2012-07-16 10:20:40
【问题描述】:

我必须将此 http 帖子从 JavaScript 转换为 Android。

我在使用cids: [] 时遇到了问题。我无法使用此符号 [ ] 创建 jsonobject。它应该是一个空数组。

这是我的 JavaScript:

var makeAjaxRequest = function () {
        Ext.getBody().mask('Loading...', 'x-mask-loading', false);
        var obj = {
            uid: 1161,
            cids: []        
        };
        Ext.Ajax.request({
            url: 'http://test.com.my',
            method: 'POST',
            params: { json: Ext.encode(obj) },
            success: function (response, opts) {
                Ext.getCmp('content').update(response.responseText);
                Ext.getCmp('status').setTitle('Static test.json file loaded');
                Ext.getBody().unmask();
                var data = Ext.decode(response.responseText);
                Ext.Msg.alert('result::', data.r[1].id, Ext.emptyFn);
            }
        });
    };

这是我的 Android 代码:

    String[] temp = null; 
JSONObject json = new JSONObject(); 
HttpPost post = new HttpPost(url); 
json.put("uid", 1161); 
json.put("cids", temp); 
List postParams = new ArrayList(); 
postParams.add(new BasicNameValuePair("json", json.toString())); 
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams); 
tv1.setText(postParams.toString()); 
post.setEntity(entity); 
post.setHeader("Accept", "application/json");
response = client.execute(post);

【问题讨论】:

    标签: javascript android json http post


    【解决方案1】:

    使用String[] temp = null; 是不对的。

    使用String[] temp = {};。这表示一个空数组。

    【讨论】:

      【解决方案2】:

      我不能用这个符号“[]”创建一个 jsonobject。

      ^^这基本上是您尝试创建的 JSON 数组。 JSONObjects 有 {},JSONArrays 有 []。

          public void writeJSON() {
          JSONObject user = new JSONObject();
          JSONObject user2;
          user2 = new JSONObject();
          try {
              user.put("dish_id", "1");
              user.put("dish_custom", "2");
              user.put("quantity", "2");
              user.put("shared", "2");
      
              user2.put("dish_id", "2");
              user2.put("dish_custom", "2");
              user2.put("quantity", "4");
              user2.put("shared", "3");
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      
          JSONArray notebookUsers = new JSONArray();
          notebookUsers.put(user);
          notebookUsers.put(user2);
          System.out.println("the JSON ARRAY is"+notebookUsers);
      

      将给出一个用户和用户2的JSON数组,符号为“[]”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多