2013-10-30 21 views
0

我在我的服務器上使用Play和Faye。 Play用於API調用,而Faye用於與客戶進行通信。來自Play的HTTPResponse字符串爲空

所以,我在服務器這種方法:

public static Result broadcast(String channel, String message) 
{ 
    try 
    { 
     FayeClient faye = new FayeClient("localhost"); 
     int code = faye.send(channel, message); 
     // print the code (prints 200). 

     return ok("Hello"); <------------ This is what we care about. 
    } 
    catch(Exception e) 
    { 
     return ok("false"); 
    } 
} 

這是客戶,這是一款Android手機上的代碼。 (這是HTTP POST方法,它發送一些東西到服務器,得到響應回 的問題是,我無法打印響應的消息。

public static String post(String url, List<BasicNameValuePair> params) 
{ 

    HttpClient httpclient = new DefaultHttpClient(); 
    String result = ""; 
    // Prepare a request object 
    HttpPost httpPost; 
    httpPost = new HttpPost(url); 
    httpPost.setHeader("Content-type", "application/json"); 
    httpPost.setHeader("Accept", "application/json"); 

    JSONObject obj = new JSONObject(); 

    try 
    { 
     for (NameValuePair pair : params) 
      obj.put(pair.getName(), pair.getValue()); 
    } 
    catch (JSONException e) 
    { 
     return e.getMessage(); 
    } 
    // Add your data 
    try 
    { 
     httpPost.setEntity(new StringEntity(obj.toString(), "UTF-8")); 
    } 
    catch (UnsupportedEncodingException e) 
    { 
     return e.getMessage(); 
    } 
HttpResponse httpResponse; 
     try 
     { 
      httpResponse = httpclient.execute(httpPost); 


      // Get hold of the response entity 
      HttpEntity entity = httpResponse.getEntity(); 

      String str = EntityUtils.toString(entity); 
      Log.e("RestClient", "result = \"" + str + "\""); // hello should be printed here?? 
} 
catch(Exception e) 
{ 
    // ... 
} 

的問題是,在logcat中,什麼是印刷是[結果=「」]我是不是做錯了什麼? 謝謝。

回答

0

使用工具如小提琴手,看看HTTP響應包含的內容。