我创建了以下函数来检查连接状态:

private void checkConnectionStatus() {
    HttpClient httpClient = new DefaultHttpClient();

    try {
      String url = "http://xxx.xxx.xxx.xxx:8000/GaitLink/"
                   + strSessionString + "/ConnectionStatus";
      Log.d("phobos", "performing get " + url);
      HttpGet method = new HttpGet(new URI(url));
      HttpResponse response = httpClient.execute(method);

      if (response != null) {
        String result = getResponse(response.getEntity());
        ...

当我关闭服务器测试执行等待很长时间在行

HttpResponse response = httpClient.execute(method);

有人知道如何设置超时以避免等待太长时间吗?

谢谢!

我一直在用WebApi开发,已经转移到WebApi2,微软已经引入了一个新的IHttpActionResult接口,似乎建议用于返回一个HttpResponseMessage。我对这个新界面的优点感到困惑。它似乎只是提供了一种稍微简单的方法来创建HttpResponseMessage。

我认为这是“为了抽象而抽象”。我遗漏了什么吗?除了节省一行代码之外,我从使用这个新接口中获得的实际优势是什么?

旧方法(WebApi):

public HttpResponseMessage Delete(int id)
{
    var status = _Repository.DeleteCustomer(id);
    if (status)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    else
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}

新方法(WebApi2):

public IHttpActionResult Delete(int id)
{
    var status = _Repository.DeleteCustomer(id);
    if (status)
    {
        //return new HttpResponseMessage(HttpStatusCode.OK);
        return Ok();
    }
    else
    {
        //throw new HttpResponseException(HttpStatusCode.NotFound);
        return NotFound();
    }
}

因此,可以尝试获取以下JSON对象:

$ curl -i -X GET http://echo.jsontest.com/key/value/anotherKey/anotherValue
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=ISO-8859-1
Date: Wed, 30 Oct 2013 22:19:10 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked

{
   "anotherKey": "anotherValue",
   "key": "value"
}
$

是否有一种方法可以使用node或express在服务器的响应中生成完全相同的正文?显然,我们可以设置报头并指出响应的内容类型将是“application/json”,但是还有不同的方法来编写/发送对象。我所看到的最常用的是使用表单的命令:

response.write(JSON.stringify(anObject));

然而,这有两点,人们可以认为它们是“问题”:

我们正在发送一个字符串。 而且,最后没有新的行字符。

另一个想法是使用命令:

response.send(anObject);

这似乎是在发送一个基于curl输出的JSON对象,类似于上面的第一个示例。但是,当在终端上再次使用curl时,正文末尾没有新的行字符。那么,如何用node或node/express在结尾追加一个新行字符来写出这样的东西呢?