Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

Passing parameters on HTTP Post Android

Working on web integration for the first time I am having a problem registering users into the system. There is an api in my localhost that does this function. But the problem is the output its returning. Does the type of parameter also matter while passing data? Like if there is was a field asking for the user's number, in HTTP POST, do I have to send the data back in integer form ?? But I did notice that the BasicNameValuePair does not allow this.

The basic program looks like this:

public JSONObject registerURL(String fname,String lname,String mail,String password,String number,String sex,int role,String dob,String horoscope,String tob,String pob){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("first_name", fname));
        params.add(new BasicNameValuePair("last_name", lname));
        params.add(new BasicNameValuePair("email",mail));
        params.add(new BasicNameValuePair("phone",number));
        params.add(new BasicNameValuePair("password",password));
        params.add(new BasicNameValuePair("sex",sex));
        params.add(new BasicNameValuePair("role", String.valueOf(role)));
        params.add(new BasicNameValuePair("date_of_birth",dob));
        params.add(new BasicNameValuePair("horoscope_id",horoscope));
        params.add(new BasicNameValuePair("time_of_birth",tob));
        params.add(new BasicNameValuePair("place_of_birth",pob));
        try {
            json = jsonParser.getJSONFromUrl(registerURL, params, POST);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return json;
    }

Where it calls

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params, int method) throws URISyntaxException {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        HttpEntity httpentity = null;

        try {
            switch (method) {
            case 1: //PUT
                HttpPut putrequest  = new HttpPut(url);
                putrequest.setEntity(new UrlEncodedFormEntity(params));
                httpResponse  = httpClient.execute(putrequest);
                httpentity = httpResponse.getEntity();
                break;
            case 2: //GET
//              String paramString = URLEncodedUtils.format(params, "utf-8");
//              url += "?" + paramString;              
//              Log.e("URL", url);
                HttpGet httpGet = new HttpGet(url);   
                httpResponse = httpClient.execute(httpGet);
                httpentity = httpResponse.getEntity();          
                break;
            case 3: //POST
                HttpPost postrequest = new HttpPost(url);
                postrequest.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(postrequest);
                httpentity = httpResponse.getEntity();

                break;
            }

            is = httpentity.getContent();


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.i("JSON HERE", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

The error is as follows:

04-22 12:20:44.745: I/JSON HERE(666): ArrayObject Object
04-22 12:20:44.745: I/JSON HERE(666): (
04-22 12:20:44.745: I/JSON HERE(666):     [storage:ArrayObject:private] => Array
04-22 12:20:44.745: I/JSON HERE(666):         (
04-22 12:20:44.745: I/JSON HERE(666):             [exception] => Exception Object
04-22 12:20:44.745: I/JSON HERE(666):                 (
04-22 12:20:44.745: I/JSON HERE(666):                     [message:protected] => Unrecognized method 'setIduser()'
04-22 12:20:44.745: I/JSON HERE(666):                     [string:Exception:private] => 
04-22 12:20:44.745: I/JSON HERE(666):                     [code:protected] => 0
04-22 12:20:44.745: I/JSON HERE(666):                     [file:protected] => C:\xampp\htdocs\blamethestars2\application\modules\api\models\ModelAbstract.php
04-22 12:20:44.745: I/JSON HERE(666):                     [line:protected] => 200
04-22 12:20:44.745: I/JSON HERE(666):                     [trace:Exception:private] => Array
04-22 12:20:44.745: I/JSON HERE(666):                         (
04-22 12:20:44.745: I/JSON HERE(666):                             [0] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                     [file] => C:\xampp\htdocs\blamethestars2\application\modules\api\controllers\UserController.php
04-22 12:20:44.745: I/JSON HERE(666):                                     [line] => 102
04-22 12:20:44.745: I/JSON HERE(666):                                     [function] => __call
04-22 12:20:44.745: I/JSON HERE(666):                                     [class] => Api_Model_ModelAbstract
04-22 12:20:44.745: I/JSON HERE(666):                                     [type] => ->
04-22 12:20:44.745: I/JSON HERE(666):                                     [args] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                         (
04-22 12:20:44.745: I/JSON HERE(666):                                             [0] => setIduser
04-22 12:20:44.745: I/JSON HERE(666):                                             [1] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                                     [0] => 1
04-22 12:20:44.745: I/JSON HERE(666):                                                 )
04-22 12:20:44.745: I/JSON HERE(666):                                         )
04-22 12:20:44.745: I/JSON HERE(666):                                 )
04-22 12:20:44.745: I/JSON HERE(666):                             [1] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                     [file] => C:\xampp\htdocs\blamethestars2\application\modules\api\controllers\UserController.php
04-22 12:20:44.745: I/JSON HERE(666):                                     [line] => 102
04-22 12:20:44.745: I/JSON HERE(666):                                     [function] => setIduser
04-22 12:20:44.745: I/JSON HERE(666):                                     [class] => Api_Model_ProfileUser
04-22 12:20:44.745: I/JSON HERE(666):                                     [type] => ->
04-22 12:20:44.745: I/JSON HERE(666):                                     [args] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                         (
04-22 12:20:44.745: I/JSON HERE(666):                                             [0] => 1
04-22 12:20:44.745: I/JSON HERE(666):                                         )
04-22 12:20:44.745: I/JSON HERE(666):                                 )
04-22 12:20:44.745: I/JSON HERE(666):                             [2] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                     [file] => C:\xampp\htdocs\blamethestars2\library\Zend\Controller\Action.php
04-22 12:20:44.745: I/JSON HERE(666):                                     [line] => 516
04-22 12:20:44.745: I/JSON HERE(666):                                     [function] => registerAction
04-22 12:20:44.745: I/JSON HERE(666):                                     [class] => Api_UserController
04-22 12:20:44.745: I/JSON HERE(666):                                     [type] => ->
04-22 12:20:44.745: I/JSON HERE(666):                                     [args] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                         (
04-22 12:20:44.745: I/JSON HERE(666):                                         )
04-22 12:20:44.745: I/JSON HERE(666):                                 )
04-22 12:20:44.745: I/JSON HERE(666):                             [3] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                     [file] => C:\xampp\htdocs\blamethestars2\library\Zend\Controller\Dispatcher\Standard.php
04-22 12:20:44.745: I/JSON HERE(666):                                     [line] => 308
04-22 12:20:44.745: I/JSON HERE(666):                                     [function] => dispatch
04-22 12:20:44.745: I/JSON HERE(666):                                     [class] => Zend_Controller_Action
04-22 12:20:44.745: I/JSON HERE(666):                                     [type] => ->
04-22 12:20:44.745: I/JSON HERE(666):                                     [args] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                         (
04-22 12:20:44.745: I/JSON HERE(666):                                             [0] => registerAction
04-22 12:20:44.745: I/JSON HERE(666):                                         )
04-22 12:20:44.745: I/JSON HERE(666):                                 )
04-22 12:20:44.745: I/JSON HERE(666):                             [4] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                     [file] => C:\xampp\htdocs\blamethestars2\library\Zend\Controller\Front.php
04-22 12:20:44.745: I/JSON HERE(666):                                     [line] => 954
04-22 12:20:44.745: I/JSON HERE(666):                                     [function] => dispatch
04-22 12:20:44.745: I/JSON HERE(666):                                     [class] => Zend_Controller_Dispatcher_Standard
04-22 12:20:44.745: I/JSON HERE(666):                                     [type] => ->
04-22 12:20:44.745: I/JSON HERE(666):                                     [args] => Array
04-22 12:20:44.745: I/JSON HERE(666):                                         (
04-22 12:20:44.745: I/JSON HERE(666):                                             [0] => Zend_Controller_Request_Http Object
04-22 12:20:44.745: I/JSON HERE(666):                                                 (
04-22 12:20:44.745: I/JSON HERE(666):                                                     [_
04-22 12:20:44.745: E/JSON Parser(666): Error parsing data org.json.JSONException: Value ArrayObject of type java.lang.String cannot be converted to JSONObject

Upvotes: 0

Views: 1134

Answers (1)

SBotirov
SBotirov

Reputation: 14138

Aha you need first create JSONArray not JSONObject. Because, you used json array.

JSONArray jsonArray = new JSONArray(jsonString);

JSONObject jsonObject = jsonArray.getJSONObject(index);

Upvotes: 1

Related Questions