Post 值使用改造
Post values using retrofit
我目前是 android 的新手。我正在使用此站点进行练习:http://dummy.restapiexample.com/,我正在尝试使用 post 请求
这是我正在使用的代码,我使用了 jsonschema2pojo
并创建了两个 类,这让我很困惑,为什么我们需要两个 类?以及如何使用它们?代码没有崩溃。但是,当我在 toast 中显示值时,我得到 null。
JsonPlaceholer.java
public interface JsonPlaceHolderApi {
@FormUrlEncoded
@POST("create")
Call<Post> createPost(@FieldMap Map<String, String> fields);
}
Mainactivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewResult;
private JsonPlaceHolderApi jsonPlaceHolderApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textViewResult = findViewById(R.id.text_view_result);
Retrofit retrofit = new Retrofit.Builder()
// .baseUrl("https://jsonplaceholder.typicode.com/")
.baseUrl("http://dummy.restapiexample.com/api/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
createPost();
}
private void createPost() {
//Post post = new Post(23, "New Title", "New Text");
Map<String, String> fields = new HashMap<>();
fields.put("name", "NewName");
fields.put("salary", "123");
fields.put("age","12");
Call<Post> call = jsonPlaceHolderApi.createPost(fields);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
Post postResponse = response.body();
String content = "";
content += "Code: " + response.code() + "\n";
content += "ID: " + postResponse.getId() + "\n";
content += "Name : " + postResponse.getName() + "\n";
content += "Salary : " + postResponse.getSalary() + "\n";
content += "Age :" + postResponse.getAge() + "\n\n";
// textViewResult2.setText(content);
Toast.makeText(MainActivity.this, "Ans::"+content, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
//textViewResult.setText(t.getMessage());
Toast.makeText(MainActivity.this, "Failed "+t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
Post.java
public class Post {
@SerializedName("name")
@Expose
private String name;
@SerializedName("salary")
@Expose
private String salary;
@SerializedName("age")
@Expose
private String age;
@SerializedName("id")
@Expose
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
PostR.java
public class PostR {
@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private Post data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Post getData() {
return data;
}
public void setData(Post data) {
this.data = data;
}
}
2 classes 用于 DTO(数据传输对象),映射来自 API eith Java 对象的响应 将响应数据映射到 Java 个对象。
Replace your code with below code, it should work:
Call<PostR> call = jsonPlaceHolderApi.createPost(fields);
call.enqueue(new Callback<PostR>() {
@Override
public void onResponse(Call<PostR> call, Response<PostR> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
PostR postRResponse = response.body();
Post postResponse = postRResponse.getData();
if(postResponse!=null) {
String content = "";
content += "Code: " + response.code() + "\n";
content += "ID: " + postResponse.getId() + "\n";
content += "Name : " + postResponse.getName() + "\n";
content += "Salary : " + postResponse.getSalary() + "\n";
content += "Age :" + postResponse.getAge() + "\n\n";
}
// textViewResult2.setText(content);
Toast.makeText(MainActivity.this, "Ans::"+content, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<PostR> call, Throwable t) {
//textViewResult.setText(t.getMessage());
Toast.makeText(MainActivity.this, "Failed "+t.getMessage(), Toast.LENGTH_LONG).show();
}
P.S - General Process you can follow to test API:
- 使用 Postman.
检查后端 (API) 是否正常工作
- 在日志中打印 response.code() 和 response.body()。
- 检查响应的结构(keys/params)是否与PostR.java相同
- 检查 response.body() 的结构 (keys/params) 是否与 Post.java class.
相同
- 如果以上所有都按预期工作,您的 Toast 应该打印正确的值。
好的,快速看一下,您的 PostR.java 是您从回调中收到的响应的 POJO class,Post.java 是 POJO class 是什么在 data
中返回 所以在你的 MainActivity.java
中你需要用 Call<PostR>
替换所有 Call<Post>
因为 PostR.java 是你用 Post 对象得到的响应它。
尝试看看是否有帮助。
编辑: 别忘了用 Callback<PostR>
和 Response<PostR>
替换 Callback<Post>
和 Response<Post>
我目前是 android 的新手。我正在使用此站点进行练习:http://dummy.restapiexample.com/,我正在尝试使用 post 请求
这是我正在使用的代码,我使用了 jsonschema2pojo
并创建了两个 类,这让我很困惑,为什么我们需要两个 类?以及如何使用它们?代码没有崩溃。但是,当我在 toast 中显示值时,我得到 null。
JsonPlaceholer.java
public interface JsonPlaceHolderApi {
@FormUrlEncoded
@POST("create")
Call<Post> createPost(@FieldMap Map<String, String> fields);
}
Mainactivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewResult;
private JsonPlaceHolderApi jsonPlaceHolderApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textViewResult = findViewById(R.id.text_view_result);
Retrofit retrofit = new Retrofit.Builder()
// .baseUrl("https://jsonplaceholder.typicode.com/")
.baseUrl("http://dummy.restapiexample.com/api/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
createPost();
}
private void createPost() {
//Post post = new Post(23, "New Title", "New Text");
Map<String, String> fields = new HashMap<>();
fields.put("name", "NewName");
fields.put("salary", "123");
fields.put("age","12");
Call<Post> call = jsonPlaceHolderApi.createPost(fields);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
Post postResponse = response.body();
String content = "";
content += "Code: " + response.code() + "\n";
content += "ID: " + postResponse.getId() + "\n";
content += "Name : " + postResponse.getName() + "\n";
content += "Salary : " + postResponse.getSalary() + "\n";
content += "Age :" + postResponse.getAge() + "\n\n";
// textViewResult2.setText(content);
Toast.makeText(MainActivity.this, "Ans::"+content, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
//textViewResult.setText(t.getMessage());
Toast.makeText(MainActivity.this, "Failed "+t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
Post.java
public class Post {
@SerializedName("name")
@Expose
private String name;
@SerializedName("salary")
@Expose
private String salary;
@SerializedName("age")
@Expose
private String age;
@SerializedName("id")
@Expose
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
PostR.java
public class PostR {
@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private Post data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Post getData() {
return data;
}
public void setData(Post data) {
this.data = data;
}
}
2 classes 用于 DTO(数据传输对象),映射来自 API eith Java 对象的响应 将响应数据映射到 Java 个对象。
Replace your code with below code, it should work:
Call<PostR> call = jsonPlaceHolderApi.createPost(fields);
call.enqueue(new Callback<PostR>() {
@Override
public void onResponse(Call<PostR> call, Response<PostR> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
PostR postRResponse = response.body();
Post postResponse = postRResponse.getData();
if(postResponse!=null) {
String content = "";
content += "Code: " + response.code() + "\n";
content += "ID: " + postResponse.getId() + "\n";
content += "Name : " + postResponse.getName() + "\n";
content += "Salary : " + postResponse.getSalary() + "\n";
content += "Age :" + postResponse.getAge() + "\n\n";
}
// textViewResult2.setText(content);
Toast.makeText(MainActivity.this, "Ans::"+content, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<PostR> call, Throwable t) {
//textViewResult.setText(t.getMessage());
Toast.makeText(MainActivity.this, "Failed "+t.getMessage(), Toast.LENGTH_LONG).show();
}
P.S - General Process you can follow to test API:
- 使用 Postman. 检查后端 (API) 是否正常工作
- 在日志中打印 response.code() 和 response.body()。
- 检查响应的结构(keys/params)是否与PostR.java相同
- 检查 response.body() 的结构 (keys/params) 是否与 Post.java class. 相同
- 如果以上所有都按预期工作,您的 Toast 应该打印正确的值。
好的,快速看一下,您的 PostR.java 是您从回调中收到的响应的 POJO class,Post.java 是 POJO class 是什么在 data
中返回 所以在你的 MainActivity.java
中你需要用 Call<PostR>
替换所有 Call<Post>
因为 PostR.java 是你用 Post 对象得到的响应它。
尝试看看是否有帮助。
编辑: 别忘了用 Callback<PostR>
和 Response<PostR>
替换 Callback<Post>
和 Response<Post>