【问题标题】:Retrofit 2 Sequential Posts改造 2 个顺序帖子
【发布时间】:2016-04-06 21:51:55
【问题描述】:

我现在正在学习 Retrofit 2。在我的应用 Intent “UploadToServer” 我需要按顺序调用三个异步任务: 1. 发布 UserName 并取回 UserId。 2. 发布地理位置和其他一些字符串数据并取回一个“报告票号”。 3.张贴一张jpg图片和票号。 我的服务器上正在运行一个 Web 服务。

在标准的手工编码 Java 中,我会使用 looper 或类似的东西。 使用 Retrofit 2 怎么能做到这一点?哦,我想让进度条动起来,尤其是在上传 jpg 图像文件时。

谢谢。

【问题讨论】:

    标签: java android retrofit2


    【解决方案1】:

    您可以使用回调链接调用:

        OkHttpClient client = new OkHttpClient.Builder().build();
    
        Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL_WEBAPI)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(client)
                        .build();
    
        mService = retrofit.create(IWebApi.class);
        ...
        mService.first_operation(...params...).enqueue(callback); 
    

    callback 是一个期望 first_operation 结果的类的实例。如果first_operation成功,则调用second_method。

        public class Example implements Callback<void> {
    
           @Override
           public void onResponse(Response<LoginResponse> response) {
              second_method();
           }
    
           @Override
           public void onFailure(Throwable t) {        
               t.printStackTrace();
           }
    }
    

    希望对你有帮助。

    编辑:

    我使用了一个不确定的 ProgressDialog。您可以在调用服务之前显示它,并在响应到达时隐藏,在 onResponse 或 onFailure 方法中。

        mProgressDialog = ProgressDialog.show(this, getString(R.string.wait_plaease),
                getString(R.string.executing_action), true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多