Android Work Manager一次性请求返回工作者的空白结果

时间:2018-08-24 07:00:51

标签: android android-workmanager

我正在使用工作管理器进行网络呼叫并获取一些数据,因为我已经使用了OneTimeRequest。该调用在worker类中响应完美,但在正在监视工作的“生命周期所有者”中返回空白结果。

workManager= WorkManager.getInstance();
    OneTimeWorkRequest.Builder encryptionWork =new OneTimeWorkRequest.Builder(NetworkWorker.class);

    getUsersWorkReq=encryptionWork.setInputData(getWorkerInput(EXPERT_LIST_REQUEST))
            .addTag(Constant.WORK_GETUSER)
            .build();

    workManager.enqueue(getUsersWorkReq);

用于观察工人阶级的回应

 workManager.getStatusById(getUsersWorkReq.getId()).observe(this, workStatus -> {
        if (workStatus != null && workStatus.getState().isFinished()) {
            String status=workStatus.getOutputData().getString(Constant.WORK_RESULT);
            String response=workStatus.getOutputData().getString(Constant.WORK_RESPONSE);
            if(status!=null && !status.equalsIgnoreCase("")){
            }
        }
    });

但是问题是,响应字符串始终为空!即使工作经理已经完成任务并且工作人员中存在网络响应,但是当我使用setOutputData时,它也会提供空白数据。

outPut = new Data.Builder()
                        .putString(Constant.WORK_RESULT,Constant.WORK_SUCCESS)
                        .putString(Constant.WORK_RESPONSE, String.valueOf(response.body()))
                        .build();
                Log.e("WORKER", "onResponse: "+ response.body().getMsg() );
                setOutputData(outPut);

工人阶级

public class NetworkWorker extends Worker {
Data outPut;
@NonNull
@Override
public Result doWork() {

        ApiService service = RetrofitInstance.getRetrofitInstance().create(ApiService.class);

        Call<ExpertListResponse> call = service.get_recommended_users();

        Log.wtf("URL Called", call.request().url() + "");

        call.enqueue(new Callback<ExpertListResponse>() {
            @Override
            public void onResponse(Call<ExpertListResponse> call, Response<ExpertListResponse> response) {
                //onFinishedListener.onFinished(requestTag, response.body() != null ? response.body().getExpertInfo() : null);
                Data outPut = new Data.Builder()
                        .putString(Constant.WORK_RESULT,Constant.WORK_SUCCESS)
                        .putString(Constant.WORK_RESPONSE, String.valueOf(response.code()))
                        .build();
                Log.e("WORKER", "onResponse: "+ response.body().getMsg() );
                setOutputData(outPut);
            }

            @Override
            public void onFailure(Call<ExpertListResponse> call, Throwable t) {
                //onFinishedListener.onFailure(requestTag,t);
                Data outPut = new Data.Builder()
                        .putString(Constant.WORK_RESULT,Constant.WORK_FAILURE)
                        .putString(Constant.WORK_RESPONSE, String.valueOf(t))
                        .build();
                setOutputData(outPut);
            }
        });

    return Result.SUCCESS;
}

}

3 个答案:

答案 0 :(得分:4)

问题是您正在执行异步改造请求时在触发Result.Success之前返回onResponse,因此没有在工作状态下设置任何数据。

许多解决方法之一是您在工作进程中发送同步改造请求,因此doWork()方法将被阻止,直到获得网络响应为止。您必须将异步改造请求更改为同步请求,类似于以下代码片段:

    public Result doWork() {
       try {
          ApiService service = RetrofitInstance.getRetrofitInstance()
                                   .create(ApiService.class);
          Call<ExpertListResponse> call = service.get_recommended_users();
          ExpertListResponse response = call.execute().body();
          Data outPut = new Data.Builder()
                    .putString(Constant.WORK_RESULT,Constant.WORK_SUCCESS)
                    .putString(Constant.WORK_RESPONSE, String.valueOf(response.code()))
                    .build();

          setOutputData(outPut);
          return Result.SUCCESS;
       } catch(Exception ex){
            Data outPut = new Data.Builder()
                    .putString(Constant.WORK_RESULT,Constant.WORK_FAILURE)
                    .putString(Constant.WORK_RESPONSE, String.valueOf(t))
                    .build();
            setOutputData(outPut);
            return Result.Failure;
       }
    }

答案 1 :(得分:1)

您还可以使用类似这样的内容:

public class NetworkWorker extends Worker {

private static final long MAX_WAIT_TIME_SECONDS = 10L;

Data outPut;
CountDownLatch latch;

@NonNull
@Override
public Result doWork() {
    // Need to wait for the onResponse() call.
    latch = new CountDownLatch(1);

    ApiService service = RetrofitInstance.getRetrofitInstance().create(ApiService.class);
    Call<ExpertListResponse> call = service.get_recommended_users();
    call.enqueue(new Callback<ExpertListResponse>() {
        @Override
        public void onResponse(Call<ExpertListResponse> call, Response<ExpertListResponse> response) {
            Data outPut = new Data.Builder()
                    .putString(Constant.WORK_RESULT,Constant.WORK_SUCCESS)
                    .putString(Constant.WORK_RESPONSE, String.valueOf(response.code()))
                    .build();
            Log.e("WORKER", "onResponse: "+ response.body().getMsg() );
            latch.countdown();
            setOutputData(outPut);
        }

        @Override
        public void onFailure(Call<ExpertListResponse> call, Throwable t) {
            Data outPut = new Data.Builder()
                    .putString(Constant.WORK_RESULT,Constant.WORK_FAILURE)
                    .putString(Constant.WORK_RESPONSE, String.valueOf(t))
                    .build();
            latch.countdown();
            setOutputData(outPut);
        }
    });

latch.await(MAX_WAIT_TIME_SECONDS, TimeUnit.SECONDS);
return Result.SUCCESS;

}

这个想法是通过Retrofit本身公开同步API,或使用CountDownLatch。请记住,当您执行此类操作时,您可能会在线程池中使用其他线程(因为Retrofit可能会使用其他线程池)。

答案 2 :(得分:0)

==========================Activity==========================


OneTimeWorkRequest movieOneTimeRequest;
WorkManager workManager;

private void callWorkerApiInBackground() {
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        Data inputData = createInputData();

        movieOneTimeRequest = new OneTimeWorkRequest.Builder(MovieWorker.class)
                .setConstraints(constraints)
                .setInputData(inputData)
                .build();
        workManager.getWorkInfoByIdLiveData(movieOneTimeRequest.getId()).observe(this, workInfo -> {
            if (workInfo != null) {
                Log.e(TAG, "WorkInfo received: state: " + workInfo.getState());
                switch(workInfo.getState()){
                    case FAILED:
                        Log.e(TAG, "OBSERVING :: fail");
                        break;
                    case BLOCKED:
                        Log.e(TAG, "OBSERVING :: blocked");
                        break;
                    case RUNNING:
                        Log.e(TAG, "OBSERVING :: running");
                        break;
                    case ENQUEUED:
                        Log.e(TAG, "OBSERVING :: enqueued");
                        break;
                    case CANCELLED:
                        Log.e(TAG, "OBSERVING :: cancelled");
                        break;
                    case SUCCEEDED:
                        Log.e(TAG, "OBSERVING :: succeeded");
                        String workManagerOutput = workInfo.getOutputData().getString(MovieWorker.KEY_OUTPUT);
                        Log.e(TAG, " workManagerOutput: " + workManagerOutput);
                        break;
                }

            }
        });

        workManager.enqueue(movieOneTimeRequest);
    }

    private Data createInputData() {
        Data.Builder builder = new Data.Builder();
        String imageUrl = "www.image.come";
        if (!TextUtils.isEmpty(imageUrl)) {
            builder.putString(MovieWorker.KEY_IMAGE,imageUrl);
        }
        return builder.build();
    }

================================== MovieWorker ============ ===========

    import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.aiplocationtest.models.MovieData;
import com.aiplocationtest.network.NetworkApiClient;

import java.util.List;

public class MovieWorker extends Worker {
    public static final String KEY_IMAGE = "image";
    public static final String KEY_OUTPUT = "output";
    String TAG = MovieWorker.class.getSimpleName();
    String imageUrl;

    public MovieWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        imageUrl = workerParams.getInputData().getString(KEY_IMAGE);
        Log.e(TAG, " imageUrl : " + imageUrl);
    }

    @NonNull
    @Override
    public Result doWork() {
        try {
            List<MovieData> response = NetworkApiClient.getMovieList().execute().body();

            Data outputData = new Data.Builder()
                    .putString(KEY_OUTPUT, response.toString())
                    .build();
            return Result.success(outputData);
//            return  Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, " callMovieListApi Exception : " + e.getMessage());
            return Result.failure();
        }

    }
}

====================== NetworkApiClient ====================

import com.aiplocationtest.models.MovieData;

import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class NetworkApiClient {


    public static Retrofit getRestAdapter() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        return new Retrofit.Builder()
                .baseUrl(HttpConstant.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static Call<List<MovieData>> getMovieList() {
        return getRestAdapter().create(GetAPi.class).getMovieList();
    }

    public interface GetAPi {

        @GET(HttpConstant.MOVIE_LIST)
        Call<List<MovieData>> getMovieList();

    }


}