2013-10-08 158 views
1

我對android很新手,試圖通過發送一個json對象來使用.net weservice。在android中發送json對象到webservice

我創建一個JSON對象,並添加所有的參數,以它像

JSONObject json = new JSONObject(); 

    json.put("name","koli"); 
    json.put("email","[email protected]"); 

,並呼籲在的AsyncTask類的doInBackground方法要求

String response = HttpClient.SendHttpPost(params[0],json); 

這就是我如何努力使請求

public static String SendHttpPost(String URL, JSONObject jsonData) { 

     Log.d("jsonData", ""+jsonData); 
     StringBuilder stringBuilder = new StringBuilder(); 
     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpPost httpPostRequest = new HttpPost(URL); 
     httpPostRequest.setHeader("User-Agent", "com.altaver.android_PostJson2"); 
     httpPostRequest.setHeader("Accept", "application/json"); 
     httpPostRequest.setHeader("Content-Type", "application/json"); 
     StringEntity se = null; 
     try { 
      se = new StringEntity(jsonData.toString(),"UTF-8"); 

      se.setContentType("application/json"); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     httpPostRequest.setEntity(se); 
     HttpResponse response = null; 
     try { 
      response = client.execute(httpPostRequest); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 

      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

在上述方法中,

StringEntity se = null; 
      try { 
       se = new StringEntity(jsonData.toString(),"UTF-8"); 

       se.setContentType("application/json"); 
      } catch (UnsupportedEncodingException e) { 

這段代碼會做。

爲什麼我無法發送我的jsonobject?

謝謝:)

+0

編碼是正確的,什麼是問題? – shem

+0

@shem JSON我從android發送的對象在服務端爲NULL – user2386771

+0

在Log中打印時,jsonData看起來是否正常? – shem

回答

2

試試這個..你doInBackground

JSONObject jObjOut = null; 

       try { 
        HttpPost request = new HttpPost(url); 
        JSONObject returnedJObject; 
        returnedJObject = new JSONObject(JSONdata.toString()); 
        JSONStringer json = new JSONStringer(); 
        StringBuilder sb=new StringBuilder(); 


        if (returnedJObject!=null) 
        { 
         Iterator<String> itKeys = returnedJObject.keys(); 
         if(itKeys.hasNext()) 
          json.object(); 
         while (itKeys.hasNext()) 
         { 
          String k=itKeys.next(); 
          json.key(k).value(returnedJObject.get(k)); 
          //Log.e("keys "+k,"value "+returnedJObject.get(k).toString()); 
         }    
        } 
        json.endObject(); 


        StringEntity entity; 

        entity = new StringEntity(json.toString()); 

        entity.setContentType("application/json;charset=UTF-8"); 
        // entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); 
        request.setHeader("Accept", "application/json"); 
        request.setEntity(entity); 

        HttpResponse response =null; 
        DefaultHttpClient httpClient = new DefaultHttpClient(); 

        HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000); 
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),50000); 

        response = httpClient.execute(request); 

        InputStream in; 
        in = response.getEntity().getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
        String line = null; 
        while((line = reader.readLine()) != null){ 
         sb.append(line); 
        } 


        return new JSONObject(sb.toString()); 
1

嘗試換款:

se = new StringEntity(jsonData.toString(),"UTF-8"); 

這樣:

se = new StringEntity(jsonData.toString());