2013-10-02 35 views
-1

我正在嘗試使用以下代碼將兩個json編碼的值發送到我的webservice。但我沒有得到任何迴應(只是空白輸出和LogCat沒有錯誤)。然而,我試圖從PHP發佈相同的參數到我的webservice使用cURL這很好。從Android進行JSON POST時的空白響應

HttpClient client = new DefaultHttpClient(); 
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
HttpResponse response; 

try { 
    json.put("name","email"); 
    json.put("email", "email"); 
    HttpPost post = new HttpPost(url); 
    post.setHeader("Content-Type", "application/json"); 
    post.setHeader("Accept-Encoding", "application/json"); 
    post.setHeader("Accept-Language", "en-US"); 

    List<NameValuePair> ad = new ArrayList<NameValuePair>(2); 
    ad.add(new BasicNameValuePair("json", json.toString())); 
    post.setEntity(new UrlEncodedFormEntity(ad)); 
    Log.i("main", "TestPOST - nVP = "+ad.toString()); 
    response = client.execute(post); 
    if(response!=null) { 
     HttpEntity entity = response.getEntity(); 
     output = EntityUtils.toString(entity,HTTP.UTF_8); //Get the data in the entity 
    }    
} catch(Exception e) { 
} 
+0

希望您已在您的清單中提供了互聯網許可。 –

回答

0

嘗試不記錄本

if (response.getStatusLine().getStatusCode() == 200) 
{ 
    HttpEntity entity = response.getEntity(); 
    json = EntityUtils.toString(entity); 
} 
0

你捕捉異常(超類)獲取你的迴應。如果在你的try塊中發生任何類型的異常,代碼將跳轉到沒有任何日誌的catch。

更改此:

catch(Exception e){ 

       } 

catch (Exception e) 
    Log.e("myappname", "exception", e); 
} 
+0

我認爲你應該提到這個評論。 –

0

如果沒有響應,你一定要檢查你捕獲異常E,因爲你沒有寫在從句中任何東西,有可能發生了什麼事情,但你沒有注意到。