java - 使用改造后值
问题描述
我目前是 android 新手。我正在使用此站点进行练习:http ://dummy.restapiexample.com/ ,我正在尝试使用这里的发布请求是我正在使用的代码,我已经使用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;
}
}
解决方案
好的,快速浏览一下,您的 PostR.java 是您从回调接收到的响应的 POJO 类,而 Post.java 是返回的 POJO 类data
所以在您MainActivity.java
需要替换所有内容Call<Post>
,Call<PostR>
因为 PostR.java 是你得到的响应是 Post 对象。
试试看这是否有帮助。
编辑:不要忘记替换Callback<Post>
and Response<Post>
with Callback<PostR>
and Response<PostR>
too
推荐阅读
- python - Python - Selenium webdriver可以附加到浏览器中现有的运行会话
- python - 当我尝试访问我的 Django 站点时出现内部服务器错误
- c - C中的多线程与多处理
- c# - 在布局文件中读取整数会话值
- python-3.x - 如何在 simulink 中运行经过训练的 keras 模型?
- python - 为什么这段代码会产生这样的输出而不是这样的输出?
- sql - 在 sql 语句上收到 Null
- javascript - 当父级“选择”类时如何更改图像 src?
- javascript - 标识符后跟冒号
- c++ - 在 C++ 中,是否有任何理由生成并立即加入线程,而不是直接调用函数?