How to return JSONObject from doInBackground() method to onPostExecute() method on AsyncTask?

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

In Android app i want to return JSONObject from doInBackground() method to onPostExecute() method.
Here is the code:

private class AddAsyncTask extends AsyncTask      {          JSONObject jsonObjRecv;          String result;      @Override     protected JSONObject doInBackground(JSONObject... params) {         AssetObj assetObj = new AssetObj();         assetObj.setAssetName(txtname.getText().toString());         assetObj.setMobileNo(txtmobile.getText().toString());         assetObj.setOwnerId(myApp.getOwnerId());         assetObj.setStartTime(startTime.getText().toString());         assetObj.setEndTime(endTime.getText().toString());         assetObj.setInterval(interval.getText().toString());         JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);         return jsonObjRecv;      }      protected void onPostExecute(JSONObject obj){                     if(obj != null)                     {                         //do something                     } 

I have try this code i got error. Is it possible to return JSONObject from doInBackground() method to onPostExecute() method?

回答1:

Edited:

This could Help you,

private class AddAsyncTask extends AsyncTask  {      JSONObject jsonObjRecv;      String result;  @Override protected JSONObject doInBackground(String... params) {     AssetObj assetObj = new AssetObj();     assetObj.setAssetName(txtname.getText().toString());     assetObj.setMobileNo(txtmobile.getText().toString());     assetObj.setOwnerId(myApp.getOwnerId());     assetObj.setStartTime(startTime.getText().toString());     assetObj.setEndTime(endTime.getText().toString());     assetObj.setInterval(interval.getText().toString());     JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);  }  protected void onPostExecute(JSONObject obj){             if(obj != null)             {                 //do something             } 

Here is it clearly ,

private class AddAsyncTask extends AsyncTask

Probably you dont need to change return values and params in the method declaration.

Just create the following line

private class AddAsyncTask extends AsyncTask

the methods will be created automatically according to the params and return types you mentioned in

private class AddAsyncTask extends AsyncTask


回答2:

For AsyncTask pass T3 as JSONObject



回答3:

OK, Now look at this carefully,

private class AddAsyncTask extends AsyncTask

In your AsyncTask third Parameter is String So change it to JSONObject .

like,

private class AddAsyncTask extends AsyncTask


回答4:

Instead of

private class AddAsyncTask extends AsyncTask

change to

private class AddAsyncTask extends AsyncTask

The Actual Code

private class AddAsyncTask extends AsyncTask  {      JSONObject jsonObjRecv;      String result;  @Override protected JSONObject doInBackground(JSONObject... params) {     AssetObj assetObj = new AssetObj();     assetObj.setAssetName(txtname.getText().toString());     assetObj.setMobileNo(txtmobile.getText().toString());     assetObj.setOwnerId(myApp.getOwnerId());     assetObj.setStartTime(startTime.getText().toString());     assetObj.setEndTime(endTime.getText().toString());     assetObj.setInterval(interval.getText().toString());     JSONObject jsonObjRecv = SyncService.AddNewAssetRequest(assetObj);  }  protected void onPostExecute(JSONObject obj){             if(obj != null)             {                 //do something             } } } 

AsyncTask

  1. Params, the type of the parameters sent to the task upon execution.
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!