Android 使用 Retrofit 的范围问题
Scope issues in Android using Retrofit
我在 Android 中使用 Retrofit 时遇到变量范围问题:
在 MainActivity 中,我使用 Retrofit 将 JSON 回复获取到 POJO (ApiResponse),创建一个 extendedJourney 对象并将其添加到 extendedJourneyArrayList:
public class MainActivity extends Activity {
private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
...
getAPIReply(...){
service.getInfo(..., new getCallback());
...}
private class getCallback implements Callback<ApiResponse> {
public void success(ApiResponse apiResponse, Response response) {
try {
consumeApiData(apiResponse);
}
...
}
}
private void consumeApiData(ApiResponse apiResponse){
ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
extendedJourneyArrayList.add(extendedJourney);
}
public void getData(View view){
getAPIReply(...);
//Do stuff with the extendedJourneyArrayList
}
在 consumeApiData() 内部一切正常,即 extendedJourney 对象是从 apiResponse 和其他参数正确创建的,extendedJourneyArrayList 已使用新的 extendedJourney 正确更新。
然而,在getData(View view)中,extendedJourneyArrayList为空。
如何解决?谢谢 :D
您正在进行异步调用。
这意味着,在调用 service.getInfo(..., new getCallback()); 之后,流程将继续正常进行,直到它被回调中断。
因此,您在 getData(View v) 中编写的代码可能在收到响应之前正在执行。
所以你应该用回调上的数据做你想做的事(例如在consumeApiData(..) 在列表中添加数据后),或执行同步请求(您必须在单独的线程中执行)。
感谢@Kushtrim 的回答。为了解决我使用 AsyncTask 执行同步请求的问题,代码现在如下所示:
public class MainActivity extends Activity {
private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
...
public void getData(View view){
for(int i = 0; i < NUM_REQUESTS; i++){
new getAPIReply().execute(params);
}
}
private class getAPIReply extends AsyncTask<Params, Void, ApiResponse>{
@Override
protected ApiResponse doInBackground(Coords[] coords) {
return service.getRouteInfo(params);
}
@Override
protected void onPostExecute(ApiResponse apiResponse){
try {
consumeApiData(apiResponse);
} catch (JSONException e) {...}
}
private void consumeApiData(ApiResponse apiResponse) throws JSONException{
ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
extendedJourneyArrayList.add(extendedJourney);
if(extendedJourneyArrayList.size() == NUM_REQUESTS) {
//Do stuff
}
}
我在 Android 中使用 Retrofit 时遇到变量范围问题:
在 MainActivity 中,我使用 Retrofit 将 JSON 回复获取到 POJO (ApiResponse),创建一个 extendedJourney 对象并将其添加到 extendedJourneyArrayList:
public class MainActivity extends Activity {
private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
...
getAPIReply(...){
service.getInfo(..., new getCallback());
...}
private class getCallback implements Callback<ApiResponse> {
public void success(ApiResponse apiResponse, Response response) {
try {
consumeApiData(apiResponse);
}
...
}
}
private void consumeApiData(ApiResponse apiResponse){
ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
extendedJourneyArrayList.add(extendedJourney);
}
public void getData(View view){
getAPIReply(...);
//Do stuff with the extendedJourneyArrayList
}
在 consumeApiData() 内部一切正常,即 extendedJourney 对象是从 apiResponse 和其他参数正确创建的,extendedJourneyArrayList 已使用新的 extendedJourney 正确更新。
然而,在getData(View view)中,extendedJourneyArrayList为空。
如何解决?谢谢 :D
您正在进行异步调用。
这意味着,在调用 service.getInfo(..., new getCallback()); 之后,流程将继续正常进行,直到它被回调中断。
因此,您在 getData(View v) 中编写的代码可能在收到响应之前正在执行。
所以你应该用回调上的数据做你想做的事(例如在consumeApiData(..) 在列表中添加数据后),或执行同步请求(您必须在单独的线程中执行)。
感谢@Kushtrim 的回答。为了解决我使用 AsyncTask 执行同步请求的问题,代码现在如下所示:
public class MainActivity extends Activity {
private ArrayList<ExtendedJourney> extendedJourneyArrayList = new ArrayList<>();
...
public void getData(View view){
for(int i = 0; i < NUM_REQUESTS; i++){
new getAPIReply().execute(params);
}
}
private class getAPIReply extends AsyncTask<Params, Void, ApiResponse>{
@Override
protected ApiResponse doInBackground(Coords[] coords) {
return service.getRouteInfo(params);
}
@Override
protected void onPostExecute(ApiResponse apiResponse){
try {
consumeApiData(apiResponse);
} catch (JSONException e) {...}
}
private void consumeApiData(ApiResponse apiResponse) throws JSONException{
ExtendedJourney extendedJourney = new ExtendedJourney(apiResponse, params);
extendedJourneyArrayList.add(extendedJourney);
if(extendedJourneyArrayList.size() == NUM_REQUESTS) {
//Do stuff
}
}