Java 从AsyncTask类回调
我有一个APIHelper类,它的静态方法异步地向服务器发出请求,接收json字符串,解析并返回Object或ArrayList:Java 从AsyncTask类回调,java,android,android-asynctask,asynccallback,Java,Android,Android Asynctask,Asynccallback,我有一个APIHelper类,它的静态方法异步地向服务器发出请求,接收json字符串,解析并返回Object或ArrayList: ... public static ArrayList<Item> getItemsInCategory(int id_category) throws ExecutionException, InterruptedException, JSONException { DoRequest doRequest = new DoReq
...
public static ArrayList<Item> getItemsInCategory(int id_category) throws ExecutionException, InterruptedException, JSONException {
DoRequest doRequest = new DoRequest();
String jsonString = doRequest.execute(API_PATH + PRODUCT_SEARCH + CATEGORY_ID + id_category).get();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("products");
return Item.fromJson(jsonArray);
}
public static Item getItem(int id_item) throws ExecutionException, InterruptedException, JSONException {
DoRequest doRequest = new DoRequest();
String jsonString = doRequest.execute(API_PATH + PRODUCT_GET_INFO + id_item).get();
JSONObject jsonObject = new JSONObject(jsonString);
return Item.fromJson(jsonObject);
}
...
。。。
公共静态ArrayList getItemsInCategory(int id_category)抛出ExecutionException、InterruptedException、JSONException{
DoRequest DoRequest=新的DoRequest();
String jsonString=doRequest.execute(API_路径+产品_搜索+类别_ID+ID_类别).get();
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray JSONArray=jsonObject.getJSONArray(“产品”);
返回Item.fromJson(jsonArray);
}
公共静态项getItem(int id_Item)抛出ExecutionException、InterruptedException、JSONException{
DoRequest DoRequest=新的DoRequest();
String jsonString=doRequest.execute(API_PATH+PRODUCT_GET_INFO+id_item).GET();
JSONObject JSONObject=新的JSONObject(jsonString);
返回Item.fromJson(jsonObject);
}
...
现在,我想在不调用AsyncTask类DoRequest中的get()方法的情况下生成方法
我的DoRequest课程:
public class DoRequest extends AsyncTask<String, Void, String> {
ResultListener mResultListener;
public abstract interface ResultListener{
Object onResultAvailable(String result) throws JSONException;
}
DoRequest(ResultListener resultListener){
mResultListener = resultListener;
}
@Override
protected String doInBackground(String... URL) {
ServiceHandler serviceHandler = new ServiceHandler();
String jsonStr = serviceHandler.makeServiceCall(URL[0]);
return jsonStr;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
mResultListener.onResultAvailable(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
公共类DoRequest扩展了异步任务{
结果监听器mResultListener;
公共抽象接口ResultListener{
对象onResultAvailable(字符串结果)抛出JSONException;
}
DoRequest(ResultListener ResultListener){
mResultListener=resultListener;
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
ServiceHandler ServiceHandler=新ServiceHandler();
字符串jsonStr=serviceHandler.makeServiceCall(URL[0]);
返回jsonStr;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
试一试{
mResultListener.onResultAvailable(结果);
}捕获(JSONException e){
e、 printStackTrace();
}
}
帮助我更改APIHelper类中返回值的方法从DoRequest回调后。您可以使用类似otto的事件总线,让异步任务在onPostExecute中将事件发布到事件总线,然后让助手类将其结果返回给谁,在总线上侦听事件,然后在那里处理回调
http://square.github.io/otto/
使用这种方法的一个例子是:
首先,您必须使用自定义的post方法才能从后台发布到线程
public class MainThreadBus extends Bus {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
super.post(event);
} else {
handler.post(new Runnable() {
@Override
public void run() {
post(event);
}
});
}
}
}
现在我们已经设置好了,在调用helper类的类中,我们在总线上创建了一个寄存器并调用helper方法:
class someClass(){
///some stuff
public void performRestCall(int id_category){
bus.register(this);
ApiHelper.getItemsInCategory(id_category);
}
@Subscribe
public void restCallCompleted(GetCategoryEvent e){
ArrayList<Item> list = e.getList();
//do whatever else you need to
bus.unRegister(this);
}
}
您的解决方案最终将是这样的。请写一个例子。@user3284037。我写了一个简短的例子,这将有助于充分解释如何使用它以及如何在类中实际使用它。请记住,当您使用@Subscribe annotation时,请确保导入的是otto注释,而不是t的注释这是一个很容易犯的错误,IDE试图自动导入依赖项。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
bus.register(this);
try {
JSONObject jsonObject = new JSONObject(jsonString);
bus.post(new GetCategoryEvent( Item.fromJson(jsonObject));
} catch (JSONException e) {
e.printStackTrace();
}
}