android - 如何在 Retrofit 1.9 中执行同步 api 调用

标签 android retrofit

我在同步调用 api 时遇到 android.os.NetworkOnMainThreadException 异常。我不能使用异步调用,因为我有很多 api,而下一个 api 取决于前一个 api 调用的输出。

怎么办?

最佳答案

在你的retroft接口(interface)中声明方法如下:

@GET("/v1/user_info/{id}")
UserSummaryResponse getSummary(@Path("id") String uId);

您需要在网络请求调用中启动一个新线程,并且不要忘记try catch。

    new Thread(new Runnable() {
      @Override
      public void run() {
      try {
        GetSummaryResponse summaryResponse = HttpClientManager.getInstance().userInterface.getSummary(uId);
        userInfo = summaryResponse.userInfo;
                            setupUserInfo();
      } catch (Exception e) {
        withoutException = false;
        Log.e(getTag(), e.getMessage());
      }

 }}).start();

关于android - 如何在 Retrofit 1.9 中执行同步 api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702274/

相关文章:

java - 适用于 Android 和 Java 的 Retrofit REST 客户端不会终止

android - 使用 robolectric 测试改造 2,未调用回调

android - 改造通用响应处理程序

android - 以编程方式为 TableRow 数组设置边框

android - 在模块 jetified-google-pay-client-api-1.0.0-runtime.jar 中发现重复的类 com.google.android.aidl.BaseProxy

android - drawableEnd textview 属性在移动 android 中不起作用

android - 将音频传输到 Doorbird 设备

android - facebook 喜欢 android 中的实时 Activity 提要吗?

android - 按下状态时如何移动按钮文本

java - 附加 SSL 客户端证书 okhttp3 。握手失败