本文介绍了如何从doInBackground()方法返回的JSONObject到onPostExecute()上AsyncTask的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的应用程序,我想从返回的JSONObject doInBackground()方法 onPostExecute()方法。
这里是code:

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
                    }

我也试试这个code,我得到的错误。是否有可能从 doInBackground()方法返回的JSONObject到 onPostExecute()的方法?

I have try this code i got error. Is it possible to return JSONObject from doInBackground() method to onPostExecute() method?

推荐答案

编辑:

这可以帮助你,

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

也许你不需要改变在方法声明的返回值,而params。

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

只要创建以下行

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

方法会自动根据PARAMS创建并返回你所提到的类型

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

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

这篇关于如何从doInBackground()方法返回的JSONObject到onPostExecute()上AsyncTask的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:13