[英]Making http request using retrofit from non activity class
在開發過程中,我們已經在我的應用程序中使用了Activity-BL-DAO-DB(sqlite)。
由於需求的變化,我們必須僅從服務器使用REST服務。 我已經看到了改造。 但是我不確定如何在DAO類而不是SQL查詢中使用它。
我們研究了需要更多返工的公交車概念。 我們希望對代碼進行最少的更改以合並此更改。
如果還有其他需要,請告訴我。
例如:以下是示例流程,它將在列表中顯示技術列表。
技術活動的OnCreate方法:
techList=new ArrayList<Technology>();
techList=technologyBL.getAllTechnology(appId);
adapterTech=new TechnologyAdapter(this,new ArrayList<Technology> (techList));
listView.setAdapter(adapterTech);
技術BL:
public List<Technology> getAllTechnology(String appId) {
techList=technologyDao.getAllTechnology(appId);
// some logic
return techList;
}
技術部DAO:
public List<Technology> getAllTechnology(String appId) {
//sql queries
return techList;
}
技術型號:
class Technology{
String id,techName,techDescription;
//getters & setters
}
我必須用改造請求替換sql查詢。 我創建了以下改造類和接口:
RestClient界面:
public interface IRestClient {
@GET("/apps/{id}/technologies")
void getTechnoloies(@Path("id") String id,Callback<List<Technology>> cb);
//Remaining methods
}
RestClient:
public class RestClient {
private static IRestClient REST_CLIENT;
public static final String BASE_URL = "http://16.180.48.236:22197/api";
Context context;
static {
setupRestClient();
}
private RestClient() {}
public static RestClient get() {
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.setClient(getClient())
.setRequestInterceptor(new RequestInterceptor() {
//cache related things
})
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
REST_CLIENT = restAdapter.create(IAluWikiClient.class);
}
private static OkClient getClient(){
//cache related
}
}
我嘗試在DAO中同時使用同步/異步方法進行調用。 對於同步方法,它引發了一些與主線程相關的錯誤。對於異步方法,由於請求完成太晚,它崩潰了。
在DAO中同步通話:
techList=RestClient.get().getTecchnologies(id);
DAO中的異步調用:
RestClient.get().getTechnolgies(id,new CallBack<List<Technolgy>(){
@Override
public void success(List<Technology> technologies, Response response) {
techList=technologies;
}
@Override
public void failure(Retrofit error){}
});
您在這里有兩個選擇。
首先是在Activity中創建Retrofit回調:
RestClient.get().getTechnolgies(id,new CallBack<List<Technolgy>(){
@Override
public void success(List<Technology> technologies, Response response) {
ArrayList<Technology> techList = technologyBL.someLogic(technologies);
adapterTech=new TechnologyAdapter(this,techList);
listView.setAdapter(adapterTech);
}
@Override
public void failure(Retrofit error){}
});
請注意,您將必須將//some logic
部分提取到單獨的BL方法中。
第二個選擇是使改造API調用返回RxJava觀察,(被整合到改造):
RestClient.get().getTechnolgies(id)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Technology>>() {
@Override
public void call(List<Technology> technologies) {
ArrayList<Technology> techList = technologyBL.someLogic(technologies);
adapterTech=new TechnologyAdapter(this,techList);
listView.setAdapter(adapterTech);
}
});
在這種情況下,您的RestClient接口為:
public interface IRestClient {
@GET("/apps/{id}/technologies")
Observable<List<Technology>> getTechnologies(@Path("id") String id);
//Remaining methods
}
您可以在http://square.github.io/retrofit/的“同步VS.異步VS.可觀察”部分中了解更多信息。 另外,請參閱以下 兩個博客文章,以使您了解RxJava和Observables:
根據我的經驗,我認為它是有用創建一個Service
通過使用自定義的執行調用的API改造AsyncTask
實現。 這種模式保持在一個地方(服務)的所有數據模型的相互作用和下車在主線程中的所有API調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.