title: Work Manager: How to return result only when network request is done
body: <p>This is my worker class and I have tried it but doesn't seem to work the way I want it to. I only want to return the result of <code>doWork()</code> only when the PUT request is done. Any help would be appreciated, thanks.</p>
  
  <pre><code>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&lt;JSONObject&gt;() {
  
                  @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&lt;String, String&gt; getHeaders() throws AuthFailureError {
              return params;
          }
  
          @Override
          protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
              return super.getParams();
          }
      };
  
      VolleyHelper.getInstance(context).addToRequestQueue(jsonObjectRequest);
      return result[0];
  }
  </code></pre>
  
  <p>}</p>