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

 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());
}
});

}

我的JSONAPI代码是:

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

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

推荐答案

添加

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

在你的build.gradle档案里.

Json相关问答推荐

需要Jolt规范将具有动态字段的输入json转换为输出json

如何形成正确的JQ表达式以从JSON文件中获得准确的输出数据?

Vega-Lite(Deneb):难以将最小和最大值应用于折线图和文本标签以及线条末尾的点

有没有办法让serde_json正确/不正确地处理NaN、inf和-inf(IEEE 754特殊标准)?

使用 Power BI 中的 Deneb 视觉效果绘制面积图中的 X 轴日期

Jolt 变换以展平 json 字符串数组

如何使用jolt规范将一个对象添加到另一个对象中并删除该对象

Bicep脚本中如何设置弹性池的维护窗口?

取消嵌套数组并将数组名称添加为附加字段

JOLT 转换 - 删除 JSON 数组中的空 node

Angular 2/Web Api - json 解析错误语法错误意外结束输入

单元测试球衣 Restful Services

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

按 JSON 数据类型 postgres 排序

如何自动修复无效的 JSON 字符串?

alert Json 对象

在 HTML 数据属性上添加 JSON 是不是很糟糕?

如何使用 Gson 解码具有未知字段的 JSON?

从调试器获取 IntelliJ Idea 中的 JSON 对象

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题