retrofit踩坑记

在使用最新版本的retrofit的时候,出现几个莫名的问题。

先贴代码:

1、单击某个标签时

 public void testClk(View view) {
        System.out.println("begin......");
        Retrofit rft = new Retrofit.Builder().baseUrl("https://www.httpbin.org/").build();

        ITest itest = rft.create(ITest.class);
        final Call<ResponseBody> myIp = itest.getMyIp();
        new Thread()
        {
            @Override
            public void run(){
                try
                {
                    //String s = myIp.execute().body().toString();
                    String s = myIp.execute().body().string();
                    System.out.println("返回:" + s);

                }catch (IOException e)
                {
                    e.printStackTrace();
                }

            }
        }.start();
    }

2、ITest接口代码

package com.example.myapplication.service;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;

public interface ITest {
    @GET("ip")
    public Call<ResponseBody> getMyIp();
}

 

问题1:忘记配置权限引起。

Error: Invoke-customs are only supported starting with Android O (--min-api 26)

加上权限后,且重新新建虚拟机问题仍然存在。解决方法:

在build.gradle(app)中增加:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        ......
    }

    buildTypes {
        ......
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

增加内容参考图片:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值