2014-12-04 61 views
0

我遇到了很大的麻煩。在Android中使用CakePHP Web服務時出現空響應

我正在製作一個使用cakephp製作的本地Web服務的Android應用程序。在本地它是完美的,但現在,我把這些Web服務放在生產服務器上,當我從模擬器測試Android應用時,我得到了空響應。

我的代碼是這樣的:

 InputStream is = null; 
    String result = ""; 

    ArrayList<NameValuePair> parameters= 
      new ArrayList<NameValuePair>(); 

parameters.add(new BasicNameValuePair("username", "hello")); 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

     StrictMode.setThreadPolicy(policy); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     httppost.setEntity(new UrlEncodedFormEntity(parameters)); 

     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result=sb.toString();  

我注意到參數的服務器

回答

0

此代碼對我的作品時,我消耗了CakePHP的服務上到達爲空的內容。 responseBody包含響應字符串,在我的情況下是JSON。

public static JSONObject httpPost(String url, List<NameValuePair> params) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = createHttpClient(); //new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    String responseBody = null; 
    JSONObject jObject = null; 
    try { 
     httppost.setEntity(new UrlEncodedFormEntity(params)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     int responseCode = response.getStatusLine().getStatusCode(); 
     responseBody = EntityUtils.toString(response.getEntity()); 
     jObject = new JSONObject(responseBody); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } catch (JSONException e) { 
     // Oops 
    } catch (Exception e) { 
    } 
    return jObject; 
} 
+0

謝謝!這是一個比我的代碼更完美的代碼,現在web服務正在我的應用程序中運行,我想這是一些防火牆或其他干擾中間的東西。非常感謝你的貢獻 – 2014-12-04 22:00:45

相關問題