我是android.Following this tutorial 的新手,除了doInBackground方法的细微变化。我从Service获取数据
这是我的实施:
@Override
public List<Location> loadInBackground()
{
long t1 = System.currentTimeMillis();
/ we should initialize new arrayList every time this on function call
//List<ProposalListItem> proposals = new ArrayList<ProposalListItem>();
List<Location> locations= LocationListService.getInstance().getLocationList();
long t2 = System.currentTimeMillis();
Log.d(TAG, "loaded locations " + locations.size() + " in " + (t2 - t1) + " ms" + "locations" + locations);
return locations;
}
这是getLocationList的实现:
public List<Location> getLocationList()
{
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
//Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, WorkflowRestService
.getInstance().getRequestHeaders());
// Perform the HTTP GET request
ResponseEntity<LocationListItemHolder> responseEntity = WorkflowRestService.getInstance().getRestTemplate()
.exchange(WorkflowApp.getServicesURL() + "locations/getAllLocations", HttpMethod.POST, requestEntity, LocationListItemHolder.class);
Log.i("response Entity",""+responseEntity);
// LocationListItemHolder locationInstance = new LocationListItemHolder();
LocationListItemHolder locationInstance = responseEntity.getBody();
if("true".equals(locationInstance.getStatus()))
{Log.i("locationInstance.getLocationListItems if ",""+locationInstance.getLocationListItems());
return locationInstance.getLocationListItems();
}
else
{Log.i("locationInstance.getLocationListItems else",""+locationInstance.getLocationListItems());
return Collections.EMPTY_LIST;
}
}
,而此行的 {Log.i(&#34; locationInstance.getLocationListItems如果&#34;&#34;&#34 + locationInstance.getLocationListItems()); logcat的:01 -09 21:47:54.790:01-09 21:59:19.895:I / locationInstance.getLocationListItems if(20706):null 我在doInBackground方法上得到这个Logcat:
01-09 21:59:19.905:E / AndroidRuntime(20706):致命异常: ModernAsyncTask#3 01-09 21:59:19.905:E / AndroidRuntime(20706): java.lang.RuntimeException:执行时发生错误 doInBackground()01-09 21:59:19.905:E / AndroidRuntime(20706):at android.support.v4.content.ModernAsyncTask $ 3.done(ModernAsyncTask.java:137) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.FutureTask.run(FutureTask.java:137)01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.lang.Thread.run(Thread.java:856)01-09 21:59:19.905: E / AndroidRuntime(20706):引起:java.lang.NullPointerException 01-09 21:59:19.905:E / AndroidRuntime(20706):at com.mrfs.android.surveyapp.loader.LocationListLoader.loadInBackground(LocationListLoader.java:47) 01-09 21:59:19.905:E / AndroidRuntime(20706):at com.mrfs.android.surveyapp.loader.LocationListLoader.loadInBackground(LocationListLoader.java:1) 01-09 21:59:19.905:E / AndroidRuntime(20706):at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)01-09 21:59:19.905:E / AndroidRuntime(20706):at android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:51) 01-09 21:59:19.905:E / AndroidRuntime(20706):at android.support.v4.content.AsyncTaskLoader $ LoadTask.doInBackground(AsyncTaskLoader.java:40) 01-09 21:59:19.905:E / AndroidRuntime(20706):at android.support.v4.content.ModernAsyncTask $ 2.call(ModernAsyncTask.java:123) 01-09 21:59:19.905:E / AndroidRuntime(20706):at java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305) 01-09 21:59:19.905:E / AndroidRuntime(20706):... 4更多