繁体   English   中英

无法在 Body raw 上使用 Retrofit 发布

[英]Can't POST using Retrofit on Body raw

我想使用 body raw 在 API 上发布数据,但总是在 response.body 上得到 null 并且数据不发送。

这是我的 retrofit class:

public class ApiClient {
public static Retrofit retrofit;
private static final String BASE_URL = BuildConfig.BASE_URL;

public static Retrofit getRetrofitInstance(){
    if (retrofit == null){
        retrofit =  new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    return  retrofit;
}

}

然后是界面:

public interface SendData {
@POST("/")
Call<DataItem> sendData(@Body DataItem body);}

有 class model:

public class DataItem{
private String date;
private String imgUrl;
private double probability;
private String latitude;
private String longtitude;
private String className;

public DataItem(String date, String imgUrl, double probability, String latitude, String longtitude, String className) {
    this.date = date;
    this.imgUrl = imgUrl;
    this.probability = probability;
    this.latitude = latitude;
    this.longtitude = longtitude;
    this.className = className;
}

public String getDate(){
    return date;
}

public String getImgUrl(){
    return imgUrl;
}

public double getProbability(){
    return probability;
}

public String getLatitude(){
    return latitude;
}

public String getLongtitude(){
    return longtitude;
}

public String getClassName(){
    return className;
}

}

这里是调用 retrofit 来发布数据:

DataItem data = new DataItem(currentDate, imgUrl, probability, latitude, longitude, class_name);

                    SendData service = ApiClient.getRetrofitInstance().create(SendData.class);
                    Call<DataItem> call = service.sendData(data);
                    call.enqueue(new Callback<DataItem>() {
                        @Override
                        public void onResponse(Call<DataItem> call, Response<DataItem> response) {
                            String result = String.valueOf(response.body());
                            Log.d("MainActivity", "response = " + response.message());
                            Log.d("MainActivity", "result = " + result);
                            timingSendData();
                            Toast.makeText(MainActivity.this, "Data Send!", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<DataItem> call, Throwable t) {
                            Log.d(TAG, "onFailure: "+t.getMessage());
                            Log.d(TAG, "onFailure: "+t.getLocalizedMessage());
                            Toast.makeText(MainActivity.this, "Cannot Send Data!", Toast.LENGTH_SHORT).show();
                        }
                    });

我已经在 model 中使用了 serilaizaton,我也使用了 hashmap<string,string> 但仍然无法发布数据。 API 也在 postman 中进行了测试,并且可以正常工作

编辑:遵循@DinkarKumar 的建议后,将数据发布到 API 即可。 但这让我很困惑,响应不是在方法onResponse()上而是在方法onFailure()上,至少数据可以发布到API。 PS:我移动了API的URL

您应该将className更改为class_name ,因为这是真正的字段名称,或者如果您想坚持使用className ,那么您应该注释class_name 如下所示,该示例使用 GSON 库语法。

@SerializedName("class_name")
public String className;

而且您的响应不是 DataItem,它是在我使用 Postman 测试时Image Added

所以我认为你也应该改变

Call<DataItem> sendData(@Body DataItem body);

Call<String> sendData(@Body DataItem body);

除了@DinkarKumar 推荐的更改之外,您还应该有这个Content-Type header

@Headers({"Content-Type: application/json"})
@POST("/")
Call<String> sendData(@Body DataItem body);}

API also tested in postman and it's worked - In Postman also, if you remove the content-type header, You will get HTTP 400 and

{
    "msg": "Missing JSON in request"
}

您的回复不是 json object 而是列表。 你应该改变你的界面。 我认为这就是 onFailure() 触发的原因。

public interface SendData {
@POST
Call<List<DataItem>> sendData(@Body DataItem body);
}

或者您只需要使用 json object 包装该列表。

暂无
暂无

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

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