如何将改造的回调从 Activity 移动到 class
How to move retrofit's Callback from Activity to class
我以前在 Activity 中有我的 createBottomBar()。由于 Activity 传递了 4-5 百行,我将其移至单独的 class 但现在我不知道如何访问我的 updateMap()。
updateMap
代码为:
private void updateMap(String path) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
MyService service = restAdapter.create(MyService.class);
service.points(path, context);
}
接口在哪里:
public interface MyService {
@GET("/{point}")
void points(@Path("point") String path, MainActivity cb);
}
Where/how 我是否应该 move/change 改造回调以便让它继续工作?
PS :我知道这更像是一个 java 问题而不是 android.
您用于回调的 class 必须实现 Callback<T>
接口。有关详细信息,请参阅此处 Retrofit doc
所以回调不依赖于 Activity class 而是依赖于回调接口的实现。所以你可以把你的 updateMap()
方法放在你喜欢的任何 class 中,因为它不依赖于上下文。请参阅下面的简短示例
所以你的界面可能是这样的
public interface MyService {
@GET("/{point}")
void points(@Path("point") String path, Callback<YourClassType>);
}
并且您可以在匿名中内联定义回调实现 class
MyService service = restAdapter.create(MyService.class);
service.points(path, new Callback<YourClassType>)() {
@Override
public void success(YourClassType foo, Response response)
{
// success
}
@Override
public void failure(RetrofitError error) {
// something went wrong
}
});
希望这能解决您的问题?
编辑:另请注意,您不必在每次要执行请求时都重新创建 Rest Client。这样做一次就足够了。
因此,也许为您的 restclient 定义一个 class 对象并重用它。
public class MyRestClientClass{
//class context
MyService mService;
//helper method for service instantiation. call this method once
void initializeRestClient()
{
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
mService = restAdapter.create(MyService.class);
}
//your service request method
void updateMap()
{
mService.points(....)
}
}
要使用这个class,例如下面的activity是一个简短的伪代码
MyRestClientClass mRestClientClass = new MyRestClientClass();
//instantiate the rest client inside
mRestClientClass.initializeRestClient();
//to call your updateMap for example after a button click
yourButton.setOnClickListener(new OnClickListener() {
//note that this works the same way as the Retrofit callback
public void onClick(View v) {
//call your web service
mRestClientClass.updateMethod();
}
});
我以前在 Activity 中有我的 createBottomBar()。由于 Activity 传递了 4-5 百行,我将其移至单独的 class 但现在我不知道如何访问我的 updateMap()。
updateMap
代码为:
private void updateMap(String path) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
MyService service = restAdapter.create(MyService.class);
service.points(path, context);
}
接口在哪里:
public interface MyService {
@GET("/{point}")
void points(@Path("point") String path, MainActivity cb);
}
Where/how 我是否应该 move/change 改造回调以便让它继续工作?
PS :我知道这更像是一个 java 问题而不是 android.
您用于回调的 class 必须实现 Callback<T>
接口。有关详细信息,请参阅此处 Retrofit doc
所以回调不依赖于 Activity class 而是依赖于回调接口的实现。所以你可以把你的 updateMap()
方法放在你喜欢的任何 class 中,因为它不依赖于上下文。请参阅下面的简短示例
所以你的界面可能是这样的
public interface MyService {
@GET("/{point}")
void points(@Path("point") String path, Callback<YourClassType>);
}
并且您可以在匿名中内联定义回调实现 class
MyService service = restAdapter.create(MyService.class);
service.points(path, new Callback<YourClassType>)() {
@Override
public void success(YourClassType foo, Response response)
{
// success
}
@Override
public void failure(RetrofitError error) {
// something went wrong
}
});
希望这能解决您的问题?
编辑:另请注意,您不必在每次要执行请求时都重新创建 Rest Client。这样做一次就足够了。 因此,也许为您的 restclient 定义一个 class 对象并重用它。
public class MyRestClientClass{
//class context
MyService mService;
//helper method for service instantiation. call this method once
void initializeRestClient()
{
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
mService = restAdapter.create(MyService.class);
}
//your service request method
void updateMap()
{
mService.points(....)
}
}
要使用这个class,例如下面的activity是一个简短的伪代码
MyRestClientClass mRestClientClass = new MyRestClientClass();
//instantiate the rest client inside
mRestClientClass.initializeRestClient();
//to call your updateMap for example after a button click
yourButton.setOnClickListener(new OnClickListener() {
//note that this works the same way as the Retrofit callback
public void onClick(View v) {
//call your web service
mRestClientClass.updateMethod();
}
});