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

后端 未结 4 1111
名媛妹妹
名媛妹妹 2020-12-17 02:50

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

private cl         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-17 03:34

    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, Progress, Result >

    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

提交回复
热议问题