工作管理器:如何仅在网络请求完成时 return 结果

Work Manager: How to return result only when network request is done

这是我的工人 class,我已经尝试过了,但似乎没有按照我想要的方式工作。我只想在 PUT 请求完成时 return doWork() 的结果。如有任何帮助,我们将不胜感激。

public class MyWorker extends Worker {
private Context context;

public MyWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
    super(context, params);
    this.context = context;
}

@NonNull
@Override
public Result doWork() {
    return putData();
}

private Result putData() {
    final Result[] result = new Result[1];
    String url = "https://www.myurl.com";
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.PUT, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    if(response == null)
                        result[0] = Result.retry();
                    else
                        result[0] = Result.success();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error
                    if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                        result[0] = Result.retry();
                    } else if (error instanceof AuthFailureError) {
                        //TODO
                    } else if (error instanceof ServerError) {
                        //TODO
                        result[0] = Result.retry();
                    } else if (error instanceof NetworkError) {
                        //TODO
                        result[0] = Result.retry();
                    } else if (error instanceof ParseError) {
                        //TODO
                    }
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return params;
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return super.getParams();
        }
    };

    VolleyHelper.getInstance(context).addToRequestQueue(jsonObjectRequest);
    return result[0];
}

}

感谢@pfmaggi 我能够做到这一点

interface MyCallback{
    void onSuccess();
    void onError();
}

public class MyWorker extends ListenableWorker {
    private Context context;

    public MyWorker(
            @NonNull Context context,
            @NonNull WorkerParameters params) {
        super(context, params);
        this.context = context;
    }

     @NonNull
@Override
public ListenableFuture<Result> startWork() {
    return CallbackToFutureAdapter.getFuture(completer -> {
        MyCallback callback = new MyCallback() {
            @Override
            public void onSuccess() {
                completer.set(Result.success());
            }

            @Override
            public void onError() {
                completer.set(Result.retry());
            }
        };
        putData(callback);
        return callback;
    });

}

    private void putData(MyCallback callback) {
        String url = "https://www.myurl.com";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.PUT, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        if(response == null)
                            callback.onError();
                        else
                            callback.onSuccess();
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error
                        if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                            callback.onError();
                        } else if (error instanceof AuthFailureError) {
                            //TODO
                        } else if (error instanceof ServerError) {
                            //TODO
                            callback.onError();
                        } else if (error instanceof NetworkError) {
                            //TODO
                            callback.onError();
                        } else if (error instanceof ParseError) {
                            //TODO
                        }
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return params;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return super.getParams();
            }
        };

        VolleyHelper.getInstance(context).addToRequestQueue(jsonObjectRequest);
    }
}