简体   繁体   English

带有参数的android http帖子

[英]android http post with params

I have to convert this http post from JavaScript to Android. 我必须将此http帖子从JavaScript转换为Android。

I encountered a problem using cids: [] . 我在使用cids: []遇到了问题cids: [] I can't create a jsonobject with this symbol [ ] . 我无法使用此符号[ ]创建jsonobject。 It should be an empty array. 它应该是一个空数组。

This is my JavaScript: 这是我的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);
            }
        });
    };

This is my Android code: 这是我的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);

Using String[] temp = null; 使用String[] temp = null; isn't right. 不对

Use String[] temp = {}; 使用String[] temp = {}; . That denotes an empty array. 那表示一个空数组。

I cant create a jsonobject with this symbol "[ ]". 我无法使用此符号“ []”创建一个jsonobject。

^^That's basically a JSON Array you are trying to create. ^^这基本上是您要创建的JSON数组。 JSONObjects have {} and JSONArrays have []. 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);

Will give a JSON array of user and user2 with symbols "[]" 将给出一个用户和user2的JSON数组,其符号为“ []”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM