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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13191285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:49:33  来源:igfitidea点击:

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

javaandroid

提问by Dhana

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

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

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?

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

采纳答案by Kartihkraj Duraisamy

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>

回答by user370305

OK, Now look at this carefully,

好的,现在仔细看看这个,

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

In your AsyncTask third Parameter is StringSo change it to JSONObject.

在您的 AsyncTask 中,第三个参数是String因此将其更改为JSONObject.

like,

喜欢,

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

回答by san

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 >

AsyncTask <参数、进度、结果>

  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
  1. 参数,执行时发送到任务的参数类型。
  2. Progress,在后台计算期间发布的进度单元的类型。
  3. Result,后台计算结果的类型

回答by sujith

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

对于AsyncTask<T1, T2, T3>T3作为JSONObject