2012-03-01 17 views
0

我必須通過JSON對象與方法和多參數如何通過使用Android JSON多個參數列表,語法問題

的對象必須具有這種形式

{"method":"startSession","params":"["email" "[email protected]", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"} 

我的機器人代碼送這個對象是

HttpPost httppost = new HttpPost(BASE_URI); 

      JSONObject json = new JSONObject(); 

      String email="[email protected]"; 
      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 
      } 

但這發送對象用錯形式

{"method":"startSession","params":"[\"email\" \"[email protected]\", \"password\" \"1234\", \"restaConnesso\" \"1\", \"idClient\" \"IPHONE\"]"} 

(I已經在調試器看到這個)

如果我嘗試從一段代碼刪除值\」

String email="[email protected]"; 
      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 [email protected], password 1234, restaConnesso 1, idClient IPHONE]"} 

我該如何發送json與這個sintax?

{"method":"startSession","params":"["email" "[email protected]", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"} 

回答

1
DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpost = new HttpPost(url); 

    //Firstly declare & create a json object from the desired json as a whole. 

/* How to create Json*/ 
JSONObject jObjectData = new JSONObject(); 


    // Create Json Object using Facebook Data 
    jObjectData.put("method", "startSession"); 

ArrayList<String> list = new ArrayList<String>(); 
    list.add("email"); 
    list.add("[email protected]"); 
etc... .... 
    JSONArray jsArray = new JSONArray(list); 
    jObjectData.put("params", jsArray); 

    StringEntity stringEntity = new StringEntity(jObjectData.toString()); 
    httpost.setEntity(stringEntity); 
    httpost.setHeader("Accept", "application/json"); 
    httpost.setHeader("Content-type", "application/json"); 

    ResponseHandler responseHandler = new BasicResponseHandler(); 
    response = httpclient.execute(httpost, responseHandler); 
+0

謝謝你的答案,但我不明白,我究竟可以使用您已經發布了一段代碼。 你能告訴我更多?? ... THX – AndreaF 2012-03-01 19:24:34

+0

我已編輯的代碼理解 – 2012-03-02 17:18:51

+0

我已經嘗試過,但沒有工作... 如果我嘗試後 JSONArray jsArray =新JSONArray(打印字符串清單); jObjectData.put(「params」,jsArray); 放置 \t \t \t \t串出= jsArray.toString(); \t \t \t \t System.out。打印); 不打印任何東西 – AndreaF 2012-03-03 03:07:53

0

我不知道,但是,是不是問題的事實,你要發送一個數組而是將發送一個字符串?

原文:

{"method":"startSession", 
    "params":"["email" "[email protected]", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]"} 

應該是這樣的,而不是(陣列周圍沒有引號):

{"method":"startSession", 
    "params": ["email" "[email protected]", "password" "1234", "stayLogged" "1", "idClient" "IPHONE"]} 

編輯:其實我覺得你真正想要一個嵌套的對象:

{"method":"startSession", 
    "params": { "email": "[email protected]", 
       "password": "1234", 
       "stayLogged": "1", 
       "idClient": "IPHONE" 
       } 
    } 

不是?

編輯#2:你應該看看如何正確使用JSONArray和JSONObject,this page有一些很好的例子。

+0

是的,正確的我想要一個嵌套對象。但我不知道我怎麼能寫這個json的工作代碼{「method」:「startSession」, 「params」:{「email」:「[email protected]」, 「password」:「1234 「, 」stayLogged「:」1「, 」idClient「:」IPHONE「 } } – AndreaF 2012-03-01 20:43:44

1
/* 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","[email protected]"); 
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); 

jObjectData串看起來像下面的字符串: -

{"parameter1":"startSession", 
"parameter2": [{ "email": "[email protected]", 
      "password": "1234", 
      "stayLogged": "1", 
      "idClient": "IPHONE" 
      }] 
} 
相關問題