gpt4 book ai didi

android - java.lang.IllegalArgumentException : 'retrofit2.Response' is not a valid response body type. 你是说 ResponseBody 吗?对于方法 Api.NewLocation

转载 作者:行者123 更新时间:2023-11-29 02:24:22 27 4
gpt4 key购买 nike

我正在尝试设置要使用我的 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);
}

最佳答案

该错误/警告在 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);
}
});

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

关于android - java.lang.IllegalArgumentException : 'retrofit2.Response' is not a valid response body type. 你是说 ResponseBody 吗?对于方法 Api.NewLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53220322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com