2011-08-10 2 views
5

예를 들어이 형식의 콘텐츠를 보내야하는 경우 어떻게해야합니까? { "name1": [{ " "name11": "value11"}, { "name11": "value12"}, {name11 ":"value13 "},"name2 ": value2}아파치 httppost 콘텐츠 설정 방법 : 다른 이름 값 쌍을 가리키는 이름 값 쌍이 있음

기본 유형 설정 방법을 알고 있습니다. { "이름 1": "값 1", "NAME2"값 2}

NameValuePair[] nameValuePairs = new NameValuePair[2]; 
      nameValuePairs[0]= new BasicNameValuePair("name1", "value1"); 
      nameValuePairs[1] = new BasicNameValuePair("name2", value2); 

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

우리가 어떻게

답변

7

가,644,617를 참조하십시오 둥지를 달성 할 수질문에는 도움이 될만한 몇 가지 답변이 있습니다.

protected void sendJson(final String email, final String pwd) { 

       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 
       JSONObject json = new JSONObject(); 
       try{ 
        HttpPost post = new HttpPost(URL); 
        json.put("email", email); 
        json.put("password", pwd); 
        StringEntity se = new StringEntity("JSON: " + json.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 
        /*Checking response */ 
        if(response!=null){ 
         InputStream in = response.getEntity().getContent(); //Get the data in the entity 

       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        createDialog("Error", "Cannot Estabilish Connection"); 
       } 

      } 
+2

:) 덕분에, 첫 번째 옵션은 단지 만들 .I 나를 위해 일한 톤 :

HttpPost request = new HttpPost(serverUrl); request.setEntity(new ByteArrayEntity( postMessage.toString().getBytes("UTF8"))); HttpResponse response = client.execute(request); 

다른 대답은 당신이 같은 것을 할 수 있다고 다음은 답변 코드에서 간단한 코드 조각입니다 중첩 된 이름 쌍 값처럼 필요한 형식의 문자열 - ByteArrayEntity – Kavitha

관련 문제