Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将改装2调用与活动和片段分离。我做得对吗?_Android_Rest_Android Activity_Retrofit2 - Fatal编程技术网

Android 将改装2调用与活动和片段分离。我做得对吗?

Android 将改装2调用与活动和片段分离。我做得对吗?,android,rest,android-activity,retrofit2,Android,Rest,Android Activity,Retrofit2,我有一个翻新函数,我从不同的活动和片段中调用它,它生成相同的输出类型。举个简单的例子 retrofitService.fetchData().enqueue(new Callback<MyData>{}) 现在,在响应和失败时,我调用dataFetched() 现在,所有需要mydata的活动都实现了dataFetched接口。因此,我将改装逻辑分为单独的类。这是通过RESTAPI调用简化活动的正确方法,还是有更好的方法来实现这一点。稍后我可能会将这些响应存储到sql数据库中,在回

我有一个翻新函数,我从不同的活动和片段中调用它,它生成相同的输出类型。举个简单的例子

retrofitService.fetchData().enqueue(new Callback<MyData>{})
现在,在响应和失败时,我调用dataFetched()

现在,所有需要mydata的活动都实现了dataFetched接口。因此,我将改装逻辑分为单独的类。这是通过RESTAPI调用简化活动的正确方法,还是有更好的方法来实现这一点。稍后我可能会将这些响应存储到sql数据库中,在回答“谢谢”时也要考虑到这一点(不关心语法)

interface OnDataFetch(){
// where mydata holds the response body on success
// msg is response.ErrorBody when the response code is not 200 
// resonseType = 0 for success
// responseType = 1 for response code other than 200
// reponstType = 1 for on failure
public void dataFetched(Data mydata,ErrorMsg msg, int responseType);
}
onSuccess(.....){

response.isSuccessful(){
 responsetType = 0;
 Data mydata = response.body();
}else{
 //when response code is not 200 we get error msg from server
 responstType = 1;
 mydata = null;
 ErrorMsg msg = Gson.from(response.ErrorBody(),ErrorMsg);
}
  dataFetched(mydata,msg,responseType);
}


onFailure(....){
 responseType = 2;
 dataFetched(null,null,responseType);
}