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

In an Android application, I want to return a JSONObject from the doInBackground() method to the onPostExecute() method.
Here is the code:

 private class AddAsyncTask extends AsyncTask<String, Void, String> { 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 tried this code, I got an error. Is it possible to return the JSONObject method from the doInBackground() method to the onPostExecute() method?

+4
source share
4 answers

Edited by:

It can help you

 private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject> { 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 } 

It’s clear here

 private class AddAsyncTask extends AsyncTask<What type of input you need to pass to doInBackground(), Void, What type of return value you need to return to onPostExcute()> 

You probably don't need to change the return values ​​and parameters in the method declaration.

Just create the following line

 private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject> 

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

 private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject> 
+4
source

For AsyncTask<T1, T2, T3> pass T3 as JSONObject

+2
source

OK, now study this carefully

 private class AddAsyncTask extends AsyncTask<String, Void, String> 

In your third AsyncTask parameter is a String , so change it to JSONObject .

as

 private class AddAsyncTask extends AsyncTask<String, Void, JSONObject> 
+2
source

Instead

 private class AddAsyncTask extends AsyncTask<String, Void, String> 

change to

 private class AddAsyncTask extends AsyncTask<String, Void, JsonObject> 

Actual code

 private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject> { 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, Progress, Result>

  • Params, the type of parameters sent to the task during execution.
  • Progress, a type of progress unit published during background computing.
  • Result, type of background calculation result
+2
source

Source: https://habr.com/ru/post/1443597/


All Articles