我正在开发一个从网络检索数据的应用程序,将它们存储到设备然后读取它们。 问题是,我在异步任务中得到我的数据..而且我的应用程序在尝试向用户显示数据之前不会让任务完成。 我已经尝试过task.get()但没有结果(它只是停在那里)。
这是我的任务:
.warning @ vendor.bundle.js:11125ReactElementValidator.createElement @ vendor.bundle.js:27927render @ app.bundle.js:29792(anonymous function) @ app.bundle.js:407ReactCompositeComponentMixin._renderValidatedComponentWithoutOwnerOrContext @ vendor.bundle.js:14599ReactCompositeComponentMixin._renderValidatedComponent @ vendor.bundle.js:14619wrapper @ vendor.bundle.js:11394ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14232wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactMultiChild.Mixin.mountChildren @ vendor.bundle.js:22652ReactDOMComponent.Mixin._createContentMarkup @ vendor.bundle.js:19830ReactDOMComponent.Mixin.mountComponent @ vendor.bundle.js:19718ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14237wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactMultiChild.Mixin.mountChildren @ vendor.bundle.js:22652ReactDOMComponent.Mixin._createContentMarkup @ vendor.bundle.js:19830ReactDOMComponent.Mixin.mountComponent @ vendor.bundle.js:19718ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactMultiChild.Mixin.mountChildren @ vendor.bundle.js:22652ReactDOMComponent.Mixin._createContentMarkup @ vendor.bundle.js:19830ReactDOMComponent.Mixin.mountComponent @ vendor.bundle.js:19718ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactMultiChild.Mixin.mountChildren @ vendor.bundle.js:22652ReactDOMComponent.Mixin._createContentMarkup @ vendor.bundle.js:19830ReactDOMComponent.Mixin.mountComponent @ vendor.bundle.js:19718ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14237wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14237wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14237wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542ReactCompositeComponentMixin.mountComponent @ vendor.bundle.js:14237wrapper @ vendor.bundle.js:11394ReactReconciler.mountComponent @ vendor.bundle.js:12542mountComponentIntoNode @ vendor.bundle.js:8912Mixin.perform @ vendor.bundle.js:13649batchedMountComponentIntoNode @ vendor.bundle.js:8928Mixin.perform @ vendor.bundle.js:13649ReactDefaultBatchingStrategy.batchedUpdates @ vendor.bundle.js:19229batchedUpdates @ vendor.bundle.js:13154ReactMount._renderNewRootComponent @ vendor.bundle.js:9122wrapper @ vendor.bundle.js:11394ReactMount._renderSubtreeIntoContainer @ vendor.bundle.js:9196ReactMount.render @ vendor.bundle.js:9216wrapper @ vendor.bundle.js:11394(anonymous function) @ app.bundle.js:280(anonymous function) @ app.bundle.js:285(anonymous function) @ app.bundle.js:286__webpack_require__ @ vendor.bundle.js:1051fn @ vendor.bundle.js:596(anonymous function) @ app.bundle.js:7__webpack_require__ @ vendor.bundle.js:1051webpackJsonpCallback @ vendor.bundle.js:22(anonymous function) @ app.bundle.js:1
vendor.bundle.js:9781 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of
此任务是我的自定义异步任务的一个实例:
.
我真的不知道该怎么做......执行变得快而且得不到任何事情,因为我没有回复任何我想的。
答案 0 :(得分:1)
onPostExecute(Result),在后台计算完成后在UI线程上调用。后台计算的结果作为参数传递给此步骤。
@Override
protected void onPostExecute(String result) {
}
答案 1 :(得分:0)
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
并将其称为:
new DownloadFilesTask().execute(url1, url2, url3);
答案 2 :(得分:0)
我使用接口作为委托来执行此操作。这是一个例子:
在我的主要活动中,我有一个onClick监听器来触发我的异步调用,并在调用完成后处理一个监听器。
private void enableLocationButton(){
locationButton = (Button) findViewById(R.id.locationButton);
locationButton.setEnabled(true);
locationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, selectLocationActivity.class);
intent.putExtra("serverURL",server.getWebServerAddressField());
startActivityForResult(intent, 200);
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
if(resultCode == RESULT_OK) {
switch (requestCode){
case 100:
processServerResponse((PmsWebServer) data.getBundleExtra("server").get("server"));
break;
case 200:
processLocationResponse((PmsDataSource)data.getBundleExtra("location").get("location"));
default:processError();
}
}else{
processError();
}
}
selectLocationActivity中的某处我调用了异步调用以及处理响应的内容,请注意此类实现了异步调用中使用的接口。
public class selectLocationActivity extends ListActivity implements SoapServiceInterface{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_select);
chosenServer = this.removeURLHeader(getIntent().getStringExtra("serverURL"));
this.retrieveLocationOptionsByServer(chosenServer);
}
private void retrieveLocationOptionsByServer(String server) {
Map<String,Object> parms = new HashMap<String,Object>();
parms.put(WEB_SERVER_NAME,server);
SoapServiceObject service = new SoapServiceObject(Services.SERVICE_DETAILS,parms);
callTheService(service);
}
private void callTheService(SoapServiceObject service){
SoapServiceHelper helper = new SoapServiceHelper();
helper.delegate = thisActivity;
helper.execute(service);
}
@Override
public void serviceCallComplete(SoapObject response){
this.createClickableListOnScreen(response);
}
//...more code...//
}
serviceCallComplete由asyncTask启动。以下是该任务的代码
public class SoapServiceHelper extends AsyncTask<SoapServiceObject, Void, SoapObject>{
public SoapServiceInterface delegate = null;
private Integer RETRY_COUNT = 0;
private final Integer MAX_RETRY_COUNT = 2;
protected SoapObject doInBackground(SoapServiceObject... args){
SoapServiceObject service = args[0];
try{
service.callTheService();
}catch(Exception e){
System.out.println("An error occurred calling the service\n" + e.getMessage());
}
return service.getResponse();
//return callDateTimeService();
}
protected void onPostExecute(SoapObject result){
delegate.serviceCallComplete((SoapObject)(result.getProperty(0)));
}
}
最后这是界面
public interface SoapServiceInterface {
public void serviceCallComplete(SoapObject response);
}
我知道我正在直接从我的结果中向屏幕显示某些内容,只是将该部分保存并读取;)
答案 3 :(得分:0)
这项任务的一件事是它将东西保存到单身人士中。我设法使用保存在onResume()的单例中的网络信息调用方法。当线程结束时,它会转到onResume,一切正常!