【问题标题】:How can I perform this request using Retrofit?如何使用 Retrofit 执行此请求?
【发布时间】:2016-08-14 17:07:08
【问题描述】:

我正在尝试使用 Retrofit 执行请求,但在使用 Postman 工作时出现 404 错误:

  • Authorization 标头,值为 "key=123456789"
  • Content-Type 标头,值为 application/json

在体内:

{"notification": {"title":"Title","text":"Hello"},"to":"1234"}

响应会是这样的:

{ "multicast_id": 108,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    { "message_id": "1:08" }
  ]
}

我有这个 Retrofit 2 代码:

public interface FcmApi {

    @POST("/")
    @Headers({"Content-Type: application/json", "Authorization: key=123456789"})
    Observable<MyResponse> send(@Body String body);

}

MyResponse 类

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyResponse {
    public int success;
}

还有一个测试:

String json = "{\"notification\": " +
                "{\"title\":\"Title\",\"text\":\"Hello\"}," +
                "\"to\":\"1234\"}";
api.send(json);

但我收到 404 错误。使用 Postman 可以在相同的示例中正常工作。

【问题讨论】:

    标签: retrofit2


    【解决方案1】:

    好像您使用的是 key=123456789。您应该使用 Firebase 控制台中的服务器密钥。 "to":"1234" 也应该是您设备上的真实 Firebase 注册 ID。

    【讨论】:

    • 您正在使用 Retrofit 并从基于 Android 或 Java 的服务器发送消息?
    • 从安卓到安卓。通过 FCM REST API
    • Retrofit 是否为您提供任何调试输出?即收到什么响应?
    • 只是 404 错误请求。我在邮递员中提出了同样的要求,它确实有效。所以我猜是由于retrofit的请求定义是错误的,但我没有看到。
    • 您需要在 Retrofit 中启用登录 - 您可以在 StackOverflow 上轻松找到它。然后你会看到 Retrofit 传输的原始输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2020-11-25
    • 2019-07-24
    • 2015-02-27
    相关资源
    最近更新 更多