Android Retrofit将信息发布到api数据库

时间:2017-07-25 09:51:33

标签: java android api retrofit

如何将我的案例信息从Android设备发布到我的api路由。 目前我在Android监视器中没有得到任何响应。

我创建了一个基本的RetrofitBuilder类。 我创建了一个包含以下内容的ApiService:

@FormUrlEncoded
@POST("device")
Call<DeviceResponse> device(@Field("device_name") String device_name,
                            @Field("device_producer") String device_producer,
                            @Field("device_pixel_width") int device_pixel_width,
                            @Field("device_pixel_height") int device_pixel_height,
                            @Field("device_ratio_width") int device_ratio_width,
                            @Field("device_ratio_height") int device_ratio_height,
                            @Field("device_android_version") int device_android_version,
                            @Field("device_status") int device_status);

一个设备模型,它包含一个带有setter和getter的设备构造函数。

DeviceResponse类:

public class DeviceResponse {

    List<Device> data;

    public List<Device> getData() {
        return data;
    }

}

在我的活动中,我称之为:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

    tokenManager = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
    service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager);

           Integer id = null;
            String device_name = "samsung";
            String device_producer = "apple";
            int device_pixel_width = 12312;
            int device_pixel_height = 123;
            int device_ratio_width = 12;
            int device_ratio_height = 23;
            char device_android_version = 6;
            int device_status = 1;

            call = service.device(device_name, device_producer, device_pixel_width, device_pixel_height, device_ratio_width, device_ratio_height, device_android_version, device_status);

2 个答案:

答案 0 :(得分:2)

只需添加@POST注释 EndPoint ,如

@FormUrlEncoded
@POST("endPoint")
Call<DeviceResponse> device(@Field("device_name") String device_name,
                        @Field("device_producer") String device_producer,
                        @Field("device_pixel_width") int device_pixel_width,
                        @Field("device_pixel_height") int device_pixel_height,
                        @Field("device_ratio_width") int device_ratio_width,
                        @Field("device_ratio_height") int device_ratio_height,
                        @Field("device_android_version") int device_android_version,
                        @Field("device_status") int device_status);

点击此处了解详情:https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded

答案 1 :(得分:0)

尝试像这样添加@POST,

@FormUrlEncoded
@POST("yourURLendpoint")
Call<DeviceResponse> device(@Field("device_name") String device_name,
                            @Field("device_producer") String device_producer,
                            @Field("device_pixel_width") int device_pixel_width,
                            @Field("device_pixel_height") int device_pixel_height,
                            @Field("device_ratio_width") int device_ratio_width,
                            @Field("device_ratio_height") int device_ratio_height,
                            @Field("device_android_version") int device_android_version,
                            @Field("device_status") int device_status);