在处理来自 API 的数据时等待调用 Intent
Wait to call Intent while Processing data from API
我在尝试阻止 Activity 的意图发生时遇到问题,直到 API 调用被消耗和处理。
我有一个按钮,单击该按钮将调用两种方法。第一个方法 'gatherResponceAndProcess' 获取响应,导入数据,并将其转换为一些新对象。这部分正在工作。但是,在 gatherResponceAndProcess 完成之前调用 moveToNextActivity。我尝试了各种各样的事情,但无济于事。我不知道在哪里或需要做什么。请帮忙。
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
moveToNextActivity();
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity() {
Intent intent = new Intent(mContext, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
mContext.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations() {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
}
...
public class APICall extends AsyncTask {...
...
URL APIUrl;
int page = 1;
private Helper helper = Helper();
public INatCall(URL url) {
this.iNatUrl = url;
}
@Override
protected Object doInBackground(Object[] objects) {
return restCall(APIUrl);//Returns JSONObject
}
@Override
protected void onPostExecute(Object o) {
helper.processFinish((JSONArray) o)
}
我最后做的是调用 moveToNextActivity();来自 Helper.processFinish。这不是理想的,因为我必须传递上下文。
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
//moveToNextActivity(); <--Removed
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess(mContext);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity(Context context) {
Intent intent = new Intent(context, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
context.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations(Context context) {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
moveToNextActivity(mContext); //<-- Added Here
}
...
我在尝试阻止 Activity 的意图发生时遇到问题,直到 API 调用被消耗和处理。 我有一个按钮,单击该按钮将调用两种方法。第一个方法 'gatherResponceAndProcess' 获取响应,导入数据,并将其转换为一些新对象。这部分正在工作。但是,在 gatherResponceAndProcess 完成之前调用 moveToNextActivity。我尝试了各种各样的事情,但无济于事。我不知道在哪里或需要做什么。请帮忙。
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
moveToNextActivity();
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity() {
Intent intent = new Intent(mContext, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
mContext.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations() {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
}
...
public class APICall extends AsyncTask {...
...
URL APIUrl;
int page = 1;
private Helper helper = Helper();
public INatCall(URL url) {
this.iNatUrl = url;
}
@Override
protected Object doInBackground(Object[] objects) {
return restCall(APIUrl);//Returns JSONObject
}
@Override
protected void onPostExecute(Object o) {
helper.processFinish((JSONArray) o)
}
我最后做的是调用 moveToNextActivity();来自 Helper.processFinish。这不是理想的,因为我必须传递上下文。
public class MainActivity extends AppCompatActivity {...
...
Button.setOnClickListener(v -> {
gatherResponceAndProcess(); // <<<<<<< CALLS API AND IMPORTS DATA
//moveToNextActivity(); <--Removed
});
private void gatherResponceAndProcess() {
try {
Helper helper = new Helper(buildURL());
helper.fetchAndProcess(mContext);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
private void moveToNextActivity(Context context) {
Intent intent = new Intent(context, NextActivity.class);
Log.d(TAG, "moveToNextActivity: Creating Intent");
context.startActivity(intent);
}
...
public class Helper implements APICall.AsyncResponse {...
...
public void fetchUsersObservations(Context context) {
APICall call = new APICall(URL);
call.execute(); //Calls processFinish() when finsished
}
@Override
public void processFinish(JSONArray results) {//THIS HAS TO FINISH BEFORE moveToNextActivity() SHOULD BE CALLED
//map json to classes
arrayOfClasses(oldClasses)
//massage the data
arrayOfNewClasses.add(massagedData)
MyNewClass.importMassagedData(arrayOfNewClasses); //NextActivity Needs this
moveToNextActivity(mContext); //<-- Added Here
}
...