I'm beginner at this. I'm trying to get Response from POST request that in a JSONarray. And when i try to get response on the array, i can get the JSON object. this is my POJO
User.Java
public class User {
@SerializedName("username")
@Expose
private String user;
@SerializedName("pass")
@Expose
private String pass;
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private ArrayList<Datum> data;
public User(String user, String pass, String mesaage){
this.user = user;
this.pass = pass;
this.message = message;
}
public String getStatus() {
return status;
}
public ArrayList<Datum> getData() {
return data;
}
public void setData(ArrayList<Datum> data) {
this.data = data;
}
public String getMessage() {
return message;
}
}
Datum.Java:
public class Datum {
@SerializedName("id")
@Expose
private String id;
@SerializedName("username")
@Expose
private String username;
@SerializedName("mk_nama")
@Expose
private String mkNama;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMkNama() {
return mkNama;
}
public void setMkNama(String mkNama) {
this.mkNama = mkNama;
}
}
this is my Interface
public interface UserClient {
@FormUrlEncoded
@POST("login")
Call<User> loginAccount (
@Field("user") String username,
@Field("pass") String password
);}
and this is my MainActivity
private void sendNetworkRequest(String username, String password) {
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://prht.herokuapp.com/api/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
UserClient client = retrofit.create(UserClient.class);
Call<User> call = client.loginAccount(username,password);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
Toast.makeText(LoginActivity.this, response.body().getData(),Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(LoginActivity.this, "BAD ERROR" , Toast.LENGTH_SHORT).show();
}
});
}
I seem to can't reach the object of JSONarray in getData()
i think this maybe works but probably you should change some detail. put a breakpoint and debug to see what's happening in Variables.
Data data = response.body().getData();
Datum datum = data.get(0);
Toast.makeText(LoginActivity.this,datum.getUsername() +" , "+ datum.getMkNama(),Toast.LENGTH_SHORT).show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.