java.lang.IllegalArgumentException:'retrofit2.Response'不是有效的响应主体类型。你是说ResponseBody吗?对于方法Api.NewLocation

时间:2018-11-09 05:26:27

标签: android android-studio retrofit2

我正在尝试设置要使用我的API发送的伪数据,但不幸的是,我无法在logCat中定义错误。

Call<Response> call=RetrofitClient
                                .getmInstance()
                                .getApi()
                                .NewLocation("357040068483739","16.623463660","126.007236650",""+"2018-05-07 11:28:02","0","9090");

                                call.enqueue(new Callback<Response>() {
                                    @Override
                                    public void onResponse(Call<Response> call, Response<Response> response) {
                                        Toast.makeText(NavDrawerFleet.this, "Successful", Toast.LENGTH_SHORT).show();
                                              }

                                    @Override
                                    public void onFailure(Call<Response> call, Throwable t) {
                                        Toast.makeText(NavDrawerFleet.this`enter code here`, "Data Not Send", Toast.LENGTH_SHORT).show();
                                    }
                                });

这是我的API代码。

public interface Api {

    @FormUrlEncoded
    @POST("/api/Database/NewLocation")
    Call<Response>NewLocation(
        @Field("SerialNumber")String serialNumber,
        @Field("Coordinate1")String latitude,
        @Field("Coordinate2")String longitude,
        @Field("DateTime")String dateTime,
        @Field("Speed")String speed,
        @Field("Port")String port


    );
    @FormUrlEncoded
    @POST("/api/Registration/RegisterDevice")
    Call<Response> NewRegistered(
        @Field("SerialNumber")String serialNumber,
        @Field("FirstName") String firstName,
        @Field("LastName") String lastName,
        @Field("Status") String status
    );
}

这是我的retrofitClient类;我一直在观看Retrofit2的新实现,但是在我的应用中进行测试时,它会产生该错误,我无法确定。

public class RetrofitClient {
    private static  final  String BASE_URL="http://58.69.149.164:9114";
    private static RetrofitClient mInstance;
    private Retrofit retrofit;

    private RetrofitClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public static  synchronized  RetrofitClient getmInstance(){
        if(mInstance==null){
            mInstance=new RetrofitClient();
        }
    return mInstance;
    }
    public Api getApi(){
        return  retrofit.create(Api.class);
    }

1 个答案:

答案 0 :(得分:0)

该错误/警告完全可以解释为IMO。在您的API中,您使用的是Retrofit的Response作为返回类型,而不是ResponseBody。如下更改API类:

/* other imports*/
import okhttp3.ResponseBody;

public interface Api {

    @FormUrlEncoded
    @POST("/api/Database/NewLocation")
    Call<ResponseBody> NewLocation(     /* use Call<ResponseBody> */
        @Field("SerialNumber")String serialNumber,
        @Field("Coordinate1")String latitude,
        @Field("Coordinate2")String longitude,
        @Field("DateTime")String dateTime,
        @Field("Speed")String speed,
        @Field("Port")String port
    );

    @FormUrlEncoded
    @POST("/api/Registration/RegisterDevice")
    Call<ResponseBody> NewRegistered(      /* use Call<ResponseBody> */
        @Field("SerialNumber")String serialNumber,
        @Field("FirstName") String firstName,
        @Field("LastName") String lastName,
        @Field("Status") String status
    );
}

并按如下所示使用它:

Call<ResponseBody> call = RetrofitClient
                            .getmInstance()
                            .getApi()
                            .NewLocation(/* paramas */);

call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        // do whatever you want
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("tag", "Error ", t);
    }
});

也不要忘记在使用okhttp3.ResponseBody;类的任何地方导入ResponseBody