2014-12-04 66 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

相关问题