来自Play的HTTPResponse字符串为空

时间:2013-10-30 13:06:32

标签: java android playframework

我在我的服务器上使用Play和Faye。 Play用于API调用,而Faye用于与客户端通信。

所以,我在服务器

中使用了这种方法
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中,打印的是[result =“”]。难道我做错了什么? 谢谢。

1 个答案:

答案 0 :(得分:0)

使用Fiddler等工具,查看HTTP响应包含的内容。