【问题标题】:ServiceStack - Returning HttpStatus to Android DeviceServiceStack - 将 HttpStatus 返回到 Android 设备
【发布时间】:2013-03-04 04:20:34
【问题描述】:

我一直在使用 ServiceStack 创建一个 Android 设备可以与之通信的 REST 服务 - 到目前为止一切顺利,我可以将 DTO 发布到 REST 服务,它成功地节省了......我遇到的麻烦是发回返回状态以通知 android 应用程序发生了什么!

ResponseEntity <HttpStatus> responseEntity = restTemplate
                    .exchange (requestUrl,
                               HttpMethod.POST,
                               requestEntity,
                               HttpStatus.class);
/* Never gets to this bit, always throws a runtime exception */
Log.i (TAG, "responseEntity.getStatusCode () --> '" + responseEntity.getStatusCode () + "'");
return responseEntity.getStatusCode ();

其余服务的 C# 代码是……

public object Post(RegisteredDeviceDto registeredDeviceDto)
{
    Debug.WriteLine("RegisteredDeviceRestService::POST::RegisteredDeviceDto (RegisteredDeviceAdd)");
    if (registeredDeviceDto != null)
    {
        Debug.WriteLine("DeviceToAdd:" + registeredDeviceDto.ToString());
        RegisteredDeviceDomainObject registeredDeviceToAdd =
        _DtoToDomainObject.registeredDeviceDtoToDomainObject(registeredDeviceDto);

        _RegisteredDeviceDao.saveDomainObject(registeredDeviceToAdd);
        return new HttpResult(new object (), "JSON", HttpStatusCode.OK);
    }
}

即使只使用一个简单的“返回 HttpStatus.OK”,我的 android 应用程序中仍然会出现运行时异常,但所有内容都会发送到服务器并保存得很好......有点难倒我在响应方面遇到的问题代码只是:P

一如既往,非常感谢任何帮助!

【问题讨论】:

  • 忘了提 - 我正在使用 Spring Framework for Android 作为我的 Rest 提供者!

标签: c# android rest servicestack


【解决方案1】:

我不熟悉 Android 或 Spring,但我猜您的 ServiceStack 响应如何被转换/反序列化为 Android/Spring ResponseEntity &lt;HttpStatus&gt; 存在问题。 HttpStatus 看起来像一个 ENUM,如果我理解这里的文档 -http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/http/ResponseEntity.html-,它看起来像是您试图将响应正文转换为 HttpStatus ENUM。也许试试ResponseEntity&lt;String&gt;

【讨论】:

  • 是的,我想我也尝试过,但我只是得到一个空字符串.. 对 REST 和 Spring 来说有点新,所以毫无疑问,我正在做的事情真的很愚蠢!跨度>
  • 是的,将其更改为 String 有效,并且我能够从 ResponseEntity 获取状态代码和响应标头 :)
  • 在 C# 方面也是如此,对于 ServiceStack,我不得不使用“base.Response.StatusCode = (int)HttpStatusCode.Conflict;”更改响应代码,现在在 Android 端,我可以从响应标头中获取该状态代码
猜你喜欢
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 2013-01-24
  • 2013-07-26
  • 2017-08-03
  • 1970-01-01
相关资源
最近更新 更多