I am currently new to android . I am using this site for practice: http://dummy.restapiexample.com/, I am trying to use the post request
here is the code I am using, I have used jsonschema2pojo
and I created two classes so that has confused me, why do we need two classes? and how to use them? The code is not crashing. But , I get null when I display the values in a toast.
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;
}
}
The 2 classes are for the DTO (data transfer object), mapping the response from the API eith Java objects and mapping the response data with Java objects.
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: