android - 尝试通过改造从 JsonPlaceHolder 获取帖子,但响应未显示方法
问题描述
我是改造新手,我正在尝试从 jsonplaceholder 网站获取单个帖子,问题是我在回调的 onresponse 方法中找不到 getTitle、getBody、getId 和 getUserId。我输入了 (response.body.) ,但没有出现 getter 方法。
主要活动
public class MainActivity extends AppCompatActivity {
TextView tvText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvText=findViewById(R.id.tvText);
Retrofit retrofit= new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface= retrofit.create(ApiInterface.class);
Call<POST> call= apiInterface.getPost();
call.enqueue(new Callback<POST>() {
@Override
public void onResponse(Call<POST> call, Response<POST> response) {
response.body().
}
@Override
public void onFailure(Call<POST> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
界面
public interface ApiInterface {
@GET("posts/1")
Call<POST> getPost();
}
json键的帖子类
public class Post {
private int id;
private int userId;
private String title;
private String body;
public int getId() {
return id;
}
public int getUserId() {
return userId;
}
public String getTitle() {
return title;
}
public String getBody() {
return body;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvText"
android:layout_width="324dp"
android:layout_height="92dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
.....................
解决方案
您需要做一些小改动,调用类型应该是 Post 而不是 POST,因为您的模型类名称是 Post。
工作代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
Retrofit retrofit= new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface= retrofit.create(ApiInterface.class);
Call<Post> call= apiInterface.getPost();
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
Log.i("response_title",response.body().getTitle()+"");
Log.i("response_userId",response.body().getUserId()+"");
Log.i("response_id",response.body().getId()+"");
Log.i("response_body",response.body().getBody()+"");
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
}
});
}
public interface ApiInterface {
@GET("Posts/1")
Call<Post> getPost();
}
public class Post {
private int id;
private int userId;
private String title;
private String body;
public int getId() {
return id;
}
public int getUserId() {
return userId;
}
public String getTitle() {
return title;
}
public String getBody() {
return body;
}
}
}
推荐阅读
- typescript - 带有类型或接口的类型断言
- angular8 - 如何创建按钮作为重置按钮以重置 angular8 中的表单?
- mysql - 使用 EF 和 MySql 在生成的迁移上手动设置注释
- javascript - Jquery .css('背景', '值'); 不工作
- java - TreeSet 如何为添加维护 O(logN)?
- java - 创建无参数方法和字符串参数的区别?
- c# - 如何在 DbCommandInterceptor 中使用 Service 或 DbContext?
- java - 为什么 system.out.println 使用二维数组会出错?
- c# - 有没有办法改进这个库中的挂钩功能
- sql - 加入时的 SQL 过滤