java.lang.BootstrapMethodError: 来自调用站点 #4 bootstrap 方法的异常,当初始化 Retrofit 时 [英] java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method ,when initializing Retrofit

查看:23
本文介绍了java.lang.BootstrapMethodError: 来自调用站点 #4 bootstrap 方法的异常,当初始化 Retrofit 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用改造从https://jsonplaceholder.typicode.com/comments" 但我收到此错误:

I am using retrofit to get sample data from "https://jsonplaceholder.typicode.com/comments" but I am getting this error:

2020-03-31 16:33:12.011 8140-8140/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.retrofit_tutorial, PID: 8140
java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
    at okhttp3.internal.Util.<clinit>(Util.java:87)
    at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
    at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
    at okhttp3.HttpUrl.get(HttpUrl.java:917)
    at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)
    at com.example.retrofit_tutorial.MainActivity.onCreate(MainActivity.java:29)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)

我的 MainActivity.java 是:

My MainActivity.java is:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermissions(new String[]{Manifest.permission.INTERNET},0);

    textView = findViewById(R.id.text_view);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
    Call<List<Comments>> call= jsonPlaceHolderApi.getComments();
   call.enqueue(new Callback<List<Comments>>() {
   @Override
  public void onResponse(Call<List<Comments>> call, Response<List<Comments>> response) {
  if(!response.isSuccessful()){
  textView.setText("error :" +response.code());
  return;
  }
  List<Comments> comments =response.body();
    String answer="";
 for(Comments c:comments){
 answer = "postId"+c.getPostId()+" \n"+"id :" +c.getId()
        +"\nname"+ c.getName()+"\nemail "+c.getEmail()+"\n comment"+c.getComment()+"\n";
 }
 textView.setText(""+answer);
}

@Override
public void onFailure(Call<List<Comments>> call, Throwable t) {
textView.setText(t.getMessage());
}
});

}

我的 JsonPlaceHolderApi 代码是:

and My JsonPlaceHolderApi code is:

    public interface JsonPlaceHolderApi {
   @GET("comments")
   Call<List<Comments>> getComments();
   }

我还在清单文件中添加了 Internet 权限.但我仍然不知道为什么会出现这个错误.

I have also added Internet permission in Manifest file. But I still don't know why this error is coming.

推荐答案

add

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

在您的 build.gradle 文件中.

这篇关于java.lang.BootstrapMethodError: 来自调用站点 #4 bootstrap 方法的异常,当初始化 Retrofit 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆