在HTTP Post Android上传递参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在HTTP Post Android上传递参数相关的知识,希望对你有一定的参考价值。

首次使用Web集成我在向用户注册系统时遇到问题。我的localhost中有一个api可以执行此功能。但问题是输出它的回归。传递数据时参数的类型是否也重要?就好像有一个字段要求用户的号码一样,在HTTP POST中,我是否必须以整数形式发回数据?但我确实注意到BasicNameValuePair不允许这样做。

基本程序如下所示:

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;
    

它呼唤的地方

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;

    

错误如下:

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
答案

你需要先创建JSONArray而不是JSONObject。因为,你使用了json数组。

JSONArray jsonArray = new JSONArray(jsonString);

JSONObject jsonObject = jsonArray.getJSONObject(index);

以上是关于在HTTP Post Android上传递参数的主要内容,如果未能解决你的问题,请参考以下文章

android post方式给后台服务器传递数据

android 如何调用默认浏览器(webservice)打开网页使用post的方式传递参数。

post的参数传递长度有限制吗

什么步骤可以用post向网络传递多个参数

Android攻城狮httpClient post/get传递参数

Get与Post的小知识