Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

In Android app i want to return JSONObject from doInBackground() method to 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 have try this code i got error. Is it possible to return JSONObject from doInBackground() method to onPostExecute() method?

like image 485
Dhana Avatar asked Nov 02 '12 07:11

Dhana


4 Answers

Edited:

This could 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
            }

Here is it clearly ,

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()>

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

Just create the following line

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

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

private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>
like image 86
Kartihkraj Duraisamy Avatar answered Nov 08 '22 05:11

Kartihkraj Duraisamy


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

like image 26
sujith Avatar answered Nov 08 '22 04:11

sujith


OK, Now look at this carefully,

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

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

like,

private class AddAsyncTask extends AsyncTask<String, Void, JSONObject> 
like image 2
user370305 Avatar answered Nov 08 '22 05:11

user370305


Instead of

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

change to

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

The 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 >

  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
like image 2
san Avatar answered Nov 08 '22 05:11

san