使用来自非 activity class 的改造发出 http 请求
Making http request using retrofit from non activity class
我们在开发时在我的应用程序中使用了 Activity-BL-DAO-DB(sqlite)。
由于需求的变化,我们必须单独从服务器使用 REST 服务。我已经看过 Retrofit 了。但我不确定如何在 DAO classes 而不是 SQL 查询中使用它。
我们研究了需要更多返工的总线概念。我们希望对代码进行最少的更改以合并此更改。
如果还有什么需要,请告诉我。
例如:
以下是将在列表中显示技术列表的示例流程。
技术Activity 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 查询。我创建了以下改造 class 和接口:
RestClient 接口:
public interface IRestClient {
@GET("/apps/{id}/technologies")
void getTechnoloies(@Path("id") String id,Callback<List<Technology>> cb);
//Remaining methods
}
休息客户端:
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 中使用两种 sync/async 方法进行调用。对于同步方法,它抛出一些与主要相关的错误 thread.For 异步,它在请求延迟完成时崩溃。
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 方法中。
第二个选项是让 Retrofit API 调用 return RxJava Observable(集成到 Retrofit 中):
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/. Also, see these two 博文的 "SYNCHRONOUS VS. ASYNCHRONOUS VS. OBSERVABLE" 部分阅读更多相关信息,了解 RxJava 和 Observables:
根据我的经验,我发现创建一个 Service
很有用,它通过使用自定义 AsyncTask
实现来执行对 Retrofit API 的调用。此范例将所有数据模型交互保存在一个地方(服务),并从主线程获取所有 API 调用。
我们在开发时在我的应用程序中使用了 Activity-BL-DAO-DB(sqlite)。
由于需求的变化,我们必须单独从服务器使用 REST 服务。我已经看过 Retrofit 了。但我不确定如何在 DAO classes 而不是 SQL 查询中使用它。
我们研究了需要更多返工的总线概念。我们希望对代码进行最少的更改以合并此更改。
如果还有什么需要,请告诉我。
例如: 以下是将在列表中显示技术列表的示例流程。
技术Activity 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 查询。我创建了以下改造 class 和接口:
RestClient 接口:
public interface IRestClient {
@GET("/apps/{id}/technologies")
void getTechnoloies(@Path("id") String id,Callback<List<Technology>> cb);
//Remaining methods
}
休息客户端:
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 中使用两种 sync/async 方法进行调用。对于同步方法,它抛出一些与主要相关的错误 thread.For 异步,它在请求延迟完成时崩溃。
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 方法中。
第二个选项是让 Retrofit API 调用 return RxJava Observable(集成到 Retrofit 中):
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/. Also, see these two 博文的 "SYNCHRONOUS VS. ASYNCHRONOUS VS. OBSERVABLE" 部分阅读更多相关信息,了解 RxJava 和 Observables:
根据我的经验,我发现创建一个 Service
很有用,它通过使用自定义 AsyncTask
实现来执行对 Retrofit API 的调用。此范例将所有数据模型交互保存在一个地方(服务),并从主线程获取所有 API 调用。