【问题标题】:java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method ,when initializing Retrofitjava.lang.BootstrapMethodError:调用站点#4引导方法的异常,初始化改造时
【发布时间】:2020-07-11 22:09:34
【问题描述】:

我正在使用改造从“https://jsonplaceholder.typicode.com/comments”获取示例数据,但出现此错误:

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 是:

 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 代码是:

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

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

【问题讨论】:

    标签: android json android-studio retrofit2


    【解决方案1】:

    添加

      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    

    在您的 build.gradle 文件中。

    【讨论】:

    • 更具体地说,在android {} 应用级别内build.gradle
    • @PranavElric,你是救命稻草
    • 感谢您的帮助,节省了我的时间和精力
    • 真的很感谢有人能对此解决方案提供更多解释,它的含义以及它为什么有效?
    【解决方案2】:
      android {
      ...
       // Configure only for each module that uses Java 8
       // language features (either in its source code or
       // through dependencies).
       compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
       }
       // For Kotlin projects
       kotlinOptions {
         jvmTarget = "1.8"
       }
    }
    

    包括来自:https://stackoverflow.com/a/58182823/403126 的 kotlin 项目的答案

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多