【问题标题】:how to return from doInBackground only after the the thread inside it is finished仅在其中的线程完成后如何从doInBackground返回
【发布时间】:2017-02-14 20:21:54
【问题描述】:

我从 asynctak 启动了 Volley JsonObject 请求,我想在完成 volley 请求后从 doinBackground 返回。我知道它在成功启动线程后从 doInBackground 返回。但是Volley Thread完成后我如何返回!

在这段代码中,我得到了 Weather 对象的空值。所有功能都正常工作。

代码:

@Override
protected JSONObject doInBackground(Void... voids) {
    String url=getURL();
    final JSONObject[] mJsonWeather = {null};
    JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            mJsonWeather[0]=response;

      }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("Error","can't process json result");
        }
    });
    MySingleton.getInstance(mContext.getApplicationContext()).addToRequestQue(jsonObjectRequest);

    return mJsonWeather[0];

}



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

        Weather weather = getJsonWeatherData(jsonObjectWeather);
        setWeatherobj(weather);

}

【问题讨论】:

  • 您确实意识到 Volley 的主要目的是消除对执行 HTTP 请求的 AsyncTask 的需求,对吧?换句话说,将 AsyncTask 完全从等式中移除
  • 如果 Volley 请求 Asynctask 不应该出现在图片中,也是为什么你从 Asynctask 线程调用 Volley 线程。
  • 是的,但是假设我们在一个方法中进行 volley 操作,然后在完成请求之前它返回一个 null 的结果!
  • 你处理来自 onResponse 的凌空结果。除此之外的任何东西都是按顺序同步运行的。异步代码很难,但理解回调很重要

标签: android android-asynctask android-volley


【解决方案1】:

我认为您误解了 AsyncTaskasync 部分。这意味着它异步运行

当然你的代码会运行,但是当 Volley 在一个单独的线程中运行时,你已经转到return mJsonWeather[0];,它是空的。

不需要 AsyncTask。直接在Activity中调用这一段代码

编辑“干净的代码”

// Can define this wherever
Response.Listener<JSONObject> dataListener = new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Weather weather = getJsonWeatherData(response);
        setWeatherobj(weather);
  }
};

// This error listener is almost always the same, anyway, right?
Response.ErrorListener errorListener = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.i("Error","can't process json result");
    }
}

JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, 
    null, // Data
    dataListener, // onSuccess
    errorListener // onError
);

MySingleton.getInstance(getApplicationContext())
    .addToRequestQue(jsonObjectRequest); // Some RequestQueue

【讨论】:

  • 我刚刚检查过,但我想让我的主要活动干净。我试图通过在其他类中编写该代码来进行一些抽象。在其他班级有什么办法吗?
  • 是的,有,但你应该接受这个答案,因为它对你有用,而是用你的新问题发布一个新问题
  • 是的,但它间接回答了这个问题,请提出一个答案。
  • 提示:从 Volley 调用中提取Response.Listener&lt;JSONObject&gt;。将其移至另一个类中方法的参数。在创建 Volley 请求对象时使用该参数值。
【解决方案2】:

AsyncTask 在 .onPostExecute() 方法中返回操作结果。你应该从那里得到结果(它在主线程上执行)

【讨论】:

  • onPostExecute() 方法是在启动线程后执行的,我希望在 doInBackground() 中的凌空线程完成后启动。
  • 实际上,不,当你 .doInBackground() 完成时运行 .opPostExecute() 。也许问题是,您从 Volley 调用异步方法而不是同步方法,因此它立即返回。
  • 考虑不使用 AsyncTask,它是过时的技术,改用 rxJava 库
【解决方案3】:

Volley 已经为执行请求实现了一个单独的线程。您可以定义一个 onPostExecute (回调)并在 volley 请求完成时执行此操作。例如:

public void doRequest() {
    String url=getURL();
    final JSONObject[] mJsonWeather = {null};
    JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            mJsonWeather[0]=response;
            Weather weather = getJsonWeatherData(jsonObjectWeather);
        setWeatherobj(weather);

      }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("Error","can't process json result");
        }
    });
    MySingleton.getInstance(mContext.getApplicationContext()).addToRequestQue(jsonObjectRequest);

    return mJsonWeather[0];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多