简体   繁体   English

如何使用RETROFIT库发布原始JSON对象

[英]How to POST raw JSON OBJECT with RETROFIT library

I tried using Postman and testing with the same JSON object and it works totally fine, but it doesn't work when I use the retrofit library. 我尝试使用Postman并使用相同的JSON对象进行测试,但效果很好,但是当我使用翻新库时却无法正常工作。

I tried putting the JSON object in a Pojo class but it still doesn't work. 我尝试将JSON对象放入Pojo类中,但仍然无法正常工作。

NOTIFICATIONSERVICE CLASS 通知服务等级

public interface NotificationService {
    @Headers({
            "Content-Type:"+ApiConstants.CONTENT_TYPE,
            "Authorization:"+ApiConstants.SERVER_KEY
    })
    @POST(ApiConstants.SEND)
    Call<ResponseBody> sendNotification(@Body String body);

}

APICONSTANTS CLASS API常量类

public class ApiConstants {
    public static final String SERVER_KEY = "key=mykey";
    public static final String SEND = "send";
    public static final String BASE_URL = "https://fcm.googleapis.com/fcm/";
    public static final String CONTENT_TYPE = "application/json";
    public static final String NOTIFICATION = "{\n" +
            "    \"to\": \"/topics/MISSING\",\n" +
            "    \"data\": {\n" +
            "        \"extra_information\": \"This is some extra information\"\n" +
            "    },\n" +
            "    \"notification\": {\n" +
            "        \"title\": \"She pressed the button\",\n" +
            "        \"text\": \"She misses you\",\n" +
            "        \"click_action\": \"MAINACTIVITY\"\n" +
            "    }\n" +
            "}";
}

MAIN ACTIVITY 主要活动

private void buttonIsPressed() {
        RetrofitClient retrofitClient;
        retrofitClient = RetrofitClient.getInstance();


        Call<ResponseBody> call = retrofitClient.getNotificationService().sendNotification(ApiConstants.NOTIFICATION);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                ResponseBody result = response.body();
                String gs = new Gson().toJson(result);
                Log.d("MainActivity", "response = " + gs);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "//onFailure");
            }
        });
    }

I'm trying to send a notification to some specific users when a button is pressed. 我试图在按下按钮时向某些特定用户发送通知。 From what I can tell the only thing not working is that I give the wrong input for the body when using Call<ResponseBody> sendNotification(@Body String body) 从我可以看出的唯一不起作用的是,在使用Call<ResponseBody> sendNotification(@Body String body)时,我为主体输入了错误的信息

you should create a model class 您应该创建一个模型类

public class MyRequestBodyModel {

    @SerializedName("id")
    private int id;

    @SerializedName("name")
    private String name;

}

create a new instance of MyRequestBodyModel and pass your value 创建一个MyRequestBodyModel的新实例并传递您的值

MyRequestBodyModel model = new MyRequestBodyModel();
model.id = 1;
model.name = "john";

set instance of this model as arguments to retrofit call method 将此模型的实例设置为改造调用方法的参数

Call<ResponseBody> sendNotification(@Body MyRequestBodyModel model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM