You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Asynctask中的JSON对象返回至MainActivity(附解决方案)

解决AsyncTask结果返回至MainActivity的问题

你已经实现了AsyntaskGetWeekFood这个AsyncTask子类,并且验证了doInBackground方法中生成的jsonfood数据正确,但不知道如何将这个JSON对象传递回MainActivity。先来看下你的原代码:

int date;
private Context context;

public AsyntaskGetWeekFood(Integer week) {
    this.date = week;
}

@Override
protected JSONObject doInBackground(Integer... params) {
    JSONObject jsonfood = new JSONObject();
    Document document = null;
    try {
        document = Jsoup.connect("http://self.shahroodut.ac.ir/Ghaza.aspx?date=" + date).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 7; i++) {
        Element sob = document.select("#BulletedListC" + i).first();
        Element shab = document.select("#BulletedListS" + i).first();
        try {
            jsonfood.put("c"+i,sob.text());
            jsonfood.put("s"+i,shab.text());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return jsonfood;
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
    super.onPostExecute(jsonObject);
}

你找到的解决方案说明

你提到的在MainActivity的try-catch块中使用以下代码来获取JSON对象是可行的:

jsonObject[0] = new AsyntaskGetWeekFood(970305).execute().get();

不过需要注意:get()方法会阻塞当前线程(也就是MainActivity所在的主线程),直到AsyncTask完成后台任务。如果网络请求耗时较长,这可能会导致应用出现ANR(Application Not Responding)错误,严重影响用户体验。

更优的异步回调方案

推荐使用接口回调的方式传递结果,这是AsyncTask传递数据的标准做法,既不会阻塞主线程,还能优雅处理异常情况:

  1. 定义回调接口
public interface OnFoodDataLoadedListener {
    void onFoodDataLoaded(JSONObject jsonObject);
    void onLoadFailed(Exception e);
}
  1. 修改AsyntaskGetWeekFood类
    添加接口成员变量,并在任务完成/异常时触发回调:
int date;
private Context context;
private OnFoodDataLoadedListener listener;

// 修改构造方法,传入回调监听
public AsyntaskGetWeekFood(Integer week, OnFoodDataLoadedListener listener) {
    this.date = week;
    this.listener = listener;
}

@Override
protected JSONObject doInBackground(Integer... params) {
    JSONObject jsonfood = new JSONObject();
    Document document = null;
    try {
        document = Jsoup.connect("http://self.shahroodut.ac.ir/Ghaza.aspx?date=" + date).get();
        for (int i = 0; i < 7; i++) {
            Element sob = document.select("#BulletedListC" + i).first();
            Element shab = document.select("#BulletedListS" + i).first();
            try {
                jsonfood.put("c"+i,sob.text());
                jsonfood.put("s"+i,shab.text());
            } catch (JSONException e) {
                e.printStackTrace();
                if (listener != null) {
                    listener.onLoadFailed(e);
                }
                return null;
            }
        }
        return jsonfood;
    } catch (IOException e) {
        e.printStackTrace();
        if (listener != null) {
            listener.onLoadFailed(e);
        }
        return null;
    }
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
    super.onPostExecute(jsonObject);
    if (listener != null) {
        if (jsonObject != null) {
            listener.onFoodDataLoaded(jsonObject);
        } else {
            listener.onLoadFailed(new Exception("Failed to load food data"));
        }
    }
}
  1. 在MainActivity中使用回调
    实现接口并启动AsyncTask:
public class MainActivity extends AppCompatActivity implements OnFoodDataLoadedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 启动AsyncTask并传入回调
        new AsyntaskGetWeekFood(970305, this).execute();
    }

    @Override
    public void onFoodDataLoaded(JSONObject jsonObject) {
        // 在这里处理返回的JSON对象,比如更新UI
        Log.d("MainActivity", "Food data loaded: " + jsonObject.toString());
    }

    @Override
    public void onLoadFailed(Exception e) {
        // 处理加载失败的情况,比如显示错误提示
        Toast.makeText(this, "Load failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

这种方式下,AsyncTask完成后台任务后会自动在主线程触发回调方法,既保证了异步性,又能流畅处理结果和异常。

内容的提问来源于stack exchange,提问作者Stonefist 24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:02:09