gpt4 book ai didi

java - 使用改造过帐值

转载 作者:行者123 更新时间:2023-12-02 09:03:48 24 4
gpt4 key购买 nike

我目前是 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;
}

}

最佳答案

好的,快速浏览一下,您的 PostR.java 是您从回调收到的响应的 POJO 类,而 Post.java 是 data 中返回的 POJO 类。所以在你的 MainActivity.java您需要替换所有 Call<Post>Call<PostR>因为 PostR.java 是您通过其中的 Post 对象获得的响应。

尝试看看这是否有帮助。

编辑:不要忘记替换 Callback<Post>Response<Post>Callback<PostR>Response<PostR>也是

关于java - 使用改造过帐值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59980018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com