可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
- Params, the type of the parameters sent to the task upon execution.
- Progress, the type of the progress units published during the background computation.
- Result, the type of the result of the background computation