gpt4 book ai didi

java - 如何控制Activity方法流直到非Activity类执行网络操作

转载 作者:行者123 更新时间:2023-11-30 10:29:18 25 4
gpt4 key购买 nike

我在 Activity 中有一种方法,其中一些操作是从非 Activity 类执行的。我想知道非 Activity 类网络操作何时完成,以便我可以再次调用该方法。但是当我调用非 Activity 类时,方法会转到最后。

这是我的代码

public void somemethod(){
.
.
.
.
if (condition) {

new RegenerateToken().generate(DriverActivity.this); // calling non activity class which perform some network operation

//here i want to know that non activity class has performed the network operation so i can call this method again

}
.
.
.
log.d("method","ending");
}

非 Activity 类

public class RegenerateToken {
public void generate(Context context) {

ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
executeServerReq.execute();
executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {

@Override
public void onResponse(String objects) {
Utilz.printLog("RegenerateTokenresponse", objects);
}
};
}
}

最佳答案

你需要实现一个接口(interface)来获取回调

public class RegenerateToken {

public interface Callback {
public void onResponce(String data);
}

public void generate(Context context,final Callback callBack) {
ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
executeServerReq.execute();
executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {

@Override
public void onResponse(String objects) {
Utilz.printLog("RegenerateTokenresponse", objects);
callBack.onResponce(objects);
}};
}
}

回调实现

if(condition) {
new RegenerateToken().generate(DriverActivity.this, new Callback() {
@Override
public void onResponse(String objects) { //your data do ur processing
}
});
}

关于java - 如何控制Activity方法流直到非Activity类执行网络操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44064616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com