我有以下代码
public void callMatchListApi() {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<NewMatchList> apiList = apiService.getNewMatchListCallListCall(API_KEY);
apiList.enqueue(new Callback<NewMatchList>() {
@Override
public void onResponse(Call<NewMatchList> call, Response<NewMatchList> response) {
//NewMatchList newMatchList=response.body();
Log.d("ApiCall", String.valueOf(response.body().getMatchList().get(0).getTeam1()));
// Log.d("ApiCall_22",newMatchList.getMatchList().get(0).getTeam1());
Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<NewMatchList> call, Throwable t) {
Log.d("ApiCall_Error", t.toString());
}
});
}
这里NewMatchList
是pojo模态类。所以我需要将此NewMatchList
设置为动态对象,以便我可以在callMatchListApi()
中将其传递给可重用。
我已经尝试将NewMatchList替换为参数中的对象引用以及类名,但它会抛出错误。因为我是android的新手,任何人都可以帮我解决这个问题。
答案 0 :(得分:3)
如果您认为Object
对您没有帮助,您也可以使用泛型:
public <T> void callApi(T classz) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<T> apiList = apiService.getNewMatchListCallListCall(API_KEY);
apiList.enqueue(new Callback<T>() {
@Override
public void onResponse(Call<T> call, Response<T> response) {
//T T=response.body();
Log.d("ApiCall", String.valueOf(response.body().getMatchList().get(0).getTeam1()));
// Log.d("ApiCall_22",T.getMatchList().get(0).getTeam1());
Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
processResponse(response, classz);
}
@Override
public void onFailure(Call<T> call, Throwable t) {
Log.d("ApiCall_Error", t.toString());
}
});
}
private <T> void processResponse(Response<T> response, T classz) {
if (classz instanceof OneModel) {
processOneModelResponse(response);
}
if (classz instanceof AnotherModel) {
processOAnotherModelResponse(response);
}
}
您看到的是NewMatchList
对象,而不是T
对象,我们可以将其作为参数传递给我们。
现在您可以按如下方式调用该方法:
callApi(NewMatchList.class)
您可能想要重构的一件事是apiService.getNewMatchListCallListCall(API_KEY)
。
您可能希望在调用方法之前在某处外部提取该方法调用,因为我假设您的apiService
具有不同的Call
类型的方法。
<强> 更新:强>
您可以添加一种方法来处理您获得的每个响应的业务逻辑。类似processResponse
之类的内容,然后您将在其特定方法(instance of
,processOneModelResponse
)中处理每种特定类型的模型(使用processAnotherModelResponse
)
答案 1 :(得分:0)
对于可重用的尝试,请在响应类型中使用Object
泛型类型并尝试解析
public void callMatchListApi() {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Object> apiList = apiService.getNewMatchListCallListCall(API_KEY);
apiList.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
//NewMatchList newMatchList=response.body();
Log.d("ApiCall", String.valueOf(response.body().getMatchList().get(0).getTeam1()));
// Log.d("ApiCall_22",newMatchList.getMatchList().get(0).getTeam1());
Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
Log.d("ApiCall_Error", t.toString());
}
});
}
答案 2 :(得分:-1)
您好我建议将RxJava与Retrofit服务一起使用。
app build.gradle
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.8'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile group: 'com.squareup.retrofit2', name: 'converter-jackson', version: '2.3.0'
ApiHelper类
public class ApiHelper {
public static ApiService getService() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5,TimeUnit.MINUTES)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConstants.BASE_URL)
.addConverterFactory(JacksonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
return retrofit.create(ApiService.class);
}
}
ApiService类
public interface ApiService {
@POST("update_token.php")
@FormUrlEncoded
Single<ApiRespBean> updateToken(@Field("manufacture") String manufacture, @Field("model") String model, @Field("device_id") String device_id, @Field("fcm_token") String fcm_token);
@GET("get_all_history.php")
Single<HistoryRespBean> getAppHistory();
@POST("update_suggest_name.php")
@FormUrlEncoded
Single<ApiRespBean> updateSuggestName(@Field("image_id") int image_id,@Field("suggest_name") String suggest_name);
}
ApiRespBean类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"status_code",
"message",
"data"
})
public class ApiRespBean {
@JsonProperty("status_code")
private Integer statusCode;
@JsonProperty("message")
private String message;
@JsonProperty("data")
private Object data;
@JsonProperty("status_code")
public Integer getStatusCode() {
return statusCode;
}
@JsonProperty("status_code")
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
@JsonProperty("message")
public String getMessage() {
return message;
}
@JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
@JsonProperty("data")
public Object getData() {
return data;
}
@JsonProperty("data")
public void setData(Object data) {
this.data = data;
}
}
MainActivity类
ApiHelper.getService().updateToken(Build.MANUFACTURER, Build.MODEL, Build.ID, fcmToken)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(apiRespBean -> {
Log.d("TAG","success");
},
throwable -> {
Log.d("TAG","error");
});