guaz
guaz

Reputation: 191

android http post with params

I have to convert this http post from JavaScript to Android.

I encountered a problem using cids: []. I can't create a jsonobject with this symbol [ ]. It should be an empty array.

This is my 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:

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

Upvotes: 0

Views: 424

Answers (2)

pixelscreen
pixelscreen

Reputation: 1975

I cant create a jsonobject with this symbol "[ ]".

^^That's basically a JSON Array you are trying to create. JSONObjects have {} and JSONArrays have [].

    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 "[]"

Upvotes: 1

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46778

Using String[] temp = null; isn't right.

Use String[] temp = {};. That denotes an empty array.

Upvotes: 2

Related Questions