问题
I'm new in retrofit, I'm trying to get single post from jsonplaceholder website, the problem is that I can't find getTitle, getBody, getId, and getUserId in onresponse method in callback. I typed (response.body.) , but getter methods doesn't appear.
MainActivity
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();
}
});
}
}
Interface
public interface ApiInterface {
@GET("posts/1")
Call<POST> getPost();
}
Post class for json keys
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>
.....................
回答1:
you need to do small change, the Call type should be Post not POST, as your model class name is Post.
working code
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;
}
}
}
来源:https://stackoverflow.com/questions/59786697/trying-to-get-post-from-jsonplaceholder-by-retrofit-but-response-isnt-showing