Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用android在json中传递多个参数列表,语法问题_Android_Json - Fatal编程技术网

如何使用android在json中传递多个参数列表,语法问题

如何使用android在json中传递多个参数列表,语法问题,android,json,Android,Json,我必须传递一个带有方法和多个参数的json对象 对象必须具有此窗体 {"method":"startSession","params":"["email" "testmail@test.it", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"} {"method":"startSession","params":"["email" "testmail@test.it", "password" "1234", "stayLog

我必须传递一个带有方法和多个参数的json对象

对象必须具有此窗体

{"method":"startSession","params":"["email" "testmail@test.it", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"}
{"method":"startSession","params":"["email" "testmail@test.it", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"}
我在android中发送此对象的代码是

HttpPost httppost = new HttpPost(BASE_URI);

           JSONObject json = new JSONObject(); 

            String email="testmail@test.it";
            String emailRic="email"+" "+"\""+email+"\"";
            String password="1234";
            String passwordRic="password"+" "+"\""+password+"\"";
            String stayLogged="1";
            String stayLoggedRic="stayLogged"+" "+"\""+stayLogged+"\"";
            String idClient="ANDROID";
            String idClientRic="idClient"+" "+"\""+idClient+"\"";

            try {
                List<String> accessParameters=new ArrayList<String>();
                accessParameters.add(emailRic);
                accessParameters.add(passwordRic);
                accessParameters.add(stayLoggedRic);
                accessParameters.add(idClientRic);
                String par=accessParameters.toString();

                json.put("method", "startSession");
                json.put("params", par);                  

                JSONArray postjson=new JSONArray(); 
                postjson.put(json); 
                httppost.setHeader("json",json.toString());
                httppost.getParams().setParameter("jsonpost",postjson); 
                System.out.print(json); 
                HttpResponse response = mClient.execute(httppost); 

                if(response != null){
                    InputStream is = response.getEntity().getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuilder sb = new StringBuilder();
                    System.out.println("the answer is:\n"+sb);
                    String line = null;

                try {
                        while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
               // text = sb.toString();
                }
               // tv.setText(text);

            }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
(我在调试器中看到了这一点)

如果我试图从代码段中删除值\“

   String email="testmail@test.it";
            String emailRic="email"+" "+"\""+email+"\"";
            String password="1234";
            String passwordRic="password"+" "+"\""+password+"\"";
            String stayLogged="1";
            String stayLoggedRic="stayLogged"+" "+"\""+stayLogged+"\"";
            String idClient="ANDROID";
            String idClientRic="idClient"+" "+"\""+idClient+"\"";
发送的对象采用以下形式

  {"method":"startSession","params":"[email testmail@test.it, password 1234, restaConnesso 1, idClient IPHONE]"}
我如何将json与exacty一起发送到sintax???

DefaultHttpClient-httpclient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(url);
//首先,从所需的json作为一个整体声明并创建一个json对象。
/*如何创建Json*/
JSONObject jObjectData=新的JSONObject();
//使用Facebook数据创建Json对象
jObjectData.put(“方法”、“开始会话”);
ArrayList=新建ArrayList();
列表。添加(“电子邮件”);
列表。添加(“testmail@test.it");
等
JSONArray jsArray=新JSONArray(列表);
jObjectData.put(“params”,jsArray);
StringEntity StringEntity=新的StringEntity(jObjectData.toString());
httpost.setEntity(stringEntity);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
ResponseHandler ResponseHandler=新BasicResponseHandler();
response=httpclient.execute(httpost,responseHandler);

我不确定,但问题不在于您试图发送一个数组而不是发送一个字符串吗

原件:

   {"method":"startSession",
    "params":"["email" "testmail@test.it", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"}
应该是这样的(数组周围没有引号):

编辑:实际上我认为您实际上想要一个嵌套对象:

   {"method":"startSession",
    "params": { "email": "testmail@test.it", 
                "password": "1234", 
                "stayLogged": "1", 
                "idClient": "IPHONE"
               }
   }
没有


编辑#2:你应该看看如何正确使用JSONArray和JSONObject,有一些很好的例子。

谢谢你的回答,但我不明白如何准确地使用你发布的代码。你能告诉我更多吗???我已经尝试过了,但不起作用…如果我尝试在JSONArray=new之后打印字符串JSONArray(列表);jObjectData.put(“params”,jsArray);placing String out=jsArray.toString();System.out.print(out);不打印任何东西是的,对,我想要一个嵌套对象。但我不知道如何为这个json{“方法”:“startSession”,“params”:{“email”:testmail@test.it", "密码“:“1234”,“stayLogged:“1”,“idClient:“IPHONE”}”
/* Create Json object for set parameter*/
JSONObject jObjectData = new JSONObject();


// set your first parameter name
jObjectData.put("parameter1", "startSession");

ArrayList<JSONObject> list = new ArrayList<JSONObject>();
// for 1 parameter   
JSONObject jObjectData1 = new JSONObject();
jObjectData1.put("email","testmail@test.it");
jObjectData1.put("password","1234");
jObjectData1.put("stayLogged","1");
jObjectData1.put("idClient","IPHONE");
list.add(jObjectData1);

JSONArray jsArray = new JSONArray(list);

// set your second parameter has more value
jObjectData.put("parameter2", jsArray);
   {"method":"startSession",
    "params": ["email" "testmail@test.it", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]}
   {"method":"startSession",
    "params": { "email": "testmail@test.it", 
                "password": "1234", 
                "stayLogged": "1", 
                "idClient": "IPHONE"
               }
   }
/* Create Json object for set parameter*/
JSONObject jObjectData = new JSONObject();


// set your first parameter name
jObjectData.put("parameter1", "startSession");

ArrayList<JSONObject> list = new ArrayList<JSONObject>();
// for 1 parameter   
JSONObject jObjectData1 = new JSONObject();
jObjectData1.put("email","testmail@test.it");
jObjectData1.put("password","1234");
jObjectData1.put("stayLogged","1");
jObjectData1.put("idClient","IPHONE");
list.add(jObjectData1);

JSONArray jsArray = new JSONArray(list);

// set your second parameter has more value
jObjectData.put("parameter2", jsArray);
{"parameter1":"startSession",
"parameter2": [{ "email": "testmail@test.it", 
            "password": "1234", 
            "stayLogged": "1", 
            "idClient": "IPHONE"
           }]
}