如何从doInBackground()方法返回JSONObject到AsyncTask上的onPostExecute()方法?

时间:2012-11-02 07:49:01

标签: java android

在Android应用中,我想将JSONObject从doInBackground()方法返回到onPostExecute()方法 这是代码:

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
                    }

我试过这段代码我收到了错误。是否可以将JSONObject从doInBackground()方法返回到onPostExecute()方法?

4 个答案:

答案 0 :(得分:4)

<强>编辑:

这可以帮助你,

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
            }

这是明确的,

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

可能你不需要在方法声明中更改返回值和参数。

只需创建以下行

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

将根据您在

中提到的参数和返回类型自动创建方法
private class AddAsyncTask extends AsyncTask<String, Void, JSONOnject>

答案 1 :(得分:2)

AsyncTask<T1, T2, T3>传递T3作为JSONObject

答案 2 :(得分:2)

好的,现在仔细看看,

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

在AsyncTask中,第三个参数为String,因此请将其更改为JSONObject

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

答案 3 :(得分:2)

而不是

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

更改为

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

实际代码

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&lt;参数,进展,结果&gt;

  1. Params,执行时发送给任务的参数类型。
  2. 进度,后台计算期间发布的进度单位的类型。
  3. 结果,背景计算结果的类型