gpt4 book ai didi

android - 如何使用 rxjava 为列表中的每个项目发出改造 api 请求?

转载 作者:行者123 更新时间:2023-11-30 00:10:34 26 4
gpt4 key购买 nike

我是 RxJava 的新手,虽然我看到了多个与我要问的问题相关的问题,但我似乎无法将它们全部拼凑出来。

我有一个包含以下数据的 PostPatrol 对象:

public class PostPatrol {
String checkpoint_name;
String status;
int user;
String detail;
List<String> photos;

public PostPatrol(int cpId, String checkpoint_name, String detail, List<String> photos, String detail) {
this.cpId = cpId;
this.checkpoint_name = checkpoint_name;
this.detail = detail;
this.photos = photos;
this.status = status;
}

//getters and setters
}

我现在要做的是将本地照片列表保存到这个 PostPatrol 记录中,但在此之前我必须通过改造一张一张地上传照片,取回一个 url 并将其保存到一个列表中然后我将照片设置为 PostPatrol 记录。

一旦我为某个 PostPatrol 记录保存了所有需要的详细信息,我就会通过改造再次发送它。

目前,我是这样做的:

  1. 我将照片传递给函数以一张一张地上传图像
  2. 函数是这样的:

    private void uploadImage(List<String> photos, String folder, long requestId) {
    final int size = photos.size();
    final long reqId = requestId;

    for (String path : photos) {
    File file = new File(path);
    RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
    RequestBody folderName = RequestBody.create(MediaType.parse("text/plain"), folder);

    ApiEndpointInterface apiEndpointInterface = RetrofitManager.getApiInterface();

    Call<FileInfo> call4File = apiEndpointInterface.postFile(body, folderName);

    call4File.enqueue(new ApiCallback<FileInfo>() {
    @Override
    protected void do4Failure(Throwable t) {
    Log.d(TAG, t.toString());
    snackbar = Snackbar.make(viewPendingRequestLayout, R.string.sb_image_upload_error, Snackbar.LENGTH_SHORT);
    snackbar.show();
    position++;
    }

    @Override
    protected void do4PositiveResponse(Response<FileInfo> response) {
    Log.d(TAG, "Uploaded Image");
    FileInfo fileDetails = response.body();
    listUrls.add(fileDetails.getImage());
    position++;
    if (position == size) {
    postRequest(reqId);
    position = 0;
    }
    }

    @Override
    protected void do4NegativeResponse(Response<FileInfo> response) {
    String bodyMsg = "";
    try {
    bodyMsg = new String(response.errorBody().bytes());
    } catch (IOException e) {
    e.printStackTrace();
    }
    Log.d(TAG, bodyMsg);
    snackbar = Snackbar.make(viewPendingRequestLayout, R.string.sb_image_upload_error, Snackbar.LENGTH_SHORT);
    snackbar.show();
    position++;
    }
    });
    }
    }

do4PositiveResponse 中,我使用局部变量来跟踪我是否已上传所有照片,然后再将它们发送到将列表保存到 PostPatrol 记录的函数。但有时,我会遇到照片根本无法上传的问题,因为它触发得太晚或太早。

  1. 这是我在postRequest()

    上的代码
    private void postRequest(long requestId) {
    if(mapIdPatrol.containsKey(requestId)){
    PostPatrol postPatrol = mapIdPatrol.get(requestId);
    postPatrol.setPhotos(listUrls);
    postPatrolRequest(postPatrol, requestId);
    }
    listUrls = new ArrayList<>();
    }
  2. 最后是我在 postPatrolRequest()

    上的代码
    private void postPatrolRequest(final PostPatrol postPatrol, final long requestId){
    ApiEndpointInterface apiEndpointInterface = RetrofitManager.getApiInterface();
    Call<ResponseId> call4Handle = apiEndpointInterface.handleCheckpoint(postPatrol);

    call4Handle.enqueue(new ApiCallback<ResponseId>() {
    @Override
    protected void do4Failure(Throwable t) {
    finishUploading();
    Log.d(TAG, t.toString());
    }
    @Override
    protected void do4PositiveResponse(Response<ResponseId> response) {
    RequestsDataSource.removeRequest(getApplication(),requestId);
    finishUploading();
    }
    @Override
    protected void do4NegativeResponse(Response<ResponseId> response) {
    finishUploading();
    String bodyMsg = "";
    try {
    bodyMsg = new String(response.errorBody().bytes());
    } catch (IOException e) {
    e.printStackTrace();
    }
    Log.d(TAG, bodyMsg);
    snackbar = Snackbar.make(viewPendingRequestLayout, getResources().getText(R.string.sb_negative_response), Snackbar.LENGTH_SHORT);
    snackbar.show();
    }
    });

    }

我知道这是非常低效的,所以我需要你的帮助,这样我就可以尝试使用 RxJava 找到解决它的方法。谢谢。

最佳答案

操作是原子的吗?即,如果通过 Retrofit 保存某些照片失败,您还需要继续吗?

无论如何,大致的解决方案将是这样的(伪代码):

Observable<String> urls = Observable.from(listOfPhotoFilePaths)
.flatMapDelayError(path -> { return retrofit.save(readFile(path))})
.toList()

Observable<PostPatrol> pp = urls
.map(list -> { return new PostPatrol(list)})

关于android - 如何使用 rxjava 为列表中的每个项目发出改造 api 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com