I'm trying to display data into TextViews
. I'm new to Retrofit
and would appreciate any help and tip(s). The following are what I have so far.
API Client:
public class ApiClient {
public static final String BASE_URL = ("http://10.1.11.11/");
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
API Service:
public interface ApiService {
@FormUrlEncoded
@GET("/lara/getprofile.php?id=")
Call<Profile> getMyProfile(@Query("id") String id);
}
Getter and setter:
public class Profile {
@SerializedName("PID")
@Expose
private String pid;
@SerializedName("First_Name")
@Expose
private String fname;
@SerializedName("Last_Name")
@Expose
private String lname;
@SerializedName("Team_Lead")
@Expose
private String teamlead;
public Profile(String pid, String fname, String lname, String teamlead) {
this.pid = pid;
this.fname = fname;
this.lname = lname;
this.teamlead = teamlead;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getTeamlead() {
return teamlead;
}
public void setTeamlead(String teamlead) {
this.teamlead = teamlead;
}
}
Displaying data into TextViews:
private void getProfile(final String id){
ApiService apiService = ApiClient.getClient().create(ApiService.class);
Call<Profile> call = apiService.getMyProfile(id);
call.enqueue(new Callback<Profile>() {
@Override
public void onResponse(Call<Profile> call, Response<Profile> response) {
progressDialog.dismiss();
Profile p = response.body();
pid.setText(p.getPid());
fname.setText(p.getFname());
lname.setText(p.getLname());
teamlead.setText(p.getTeamlead());
}
@Override
public void onFailure(Call<Profile> call, Throwable t) {
//progressDialog.dismiss();
Toast.makeText(ProfilePage.this, "Failed to load." + t, Toast.LENGTH_LONG).show();
}
});
}
JSON fields that I'm trying to display to TextViews:
I'm getting the following error message:
[]
). I think you have two options. 1. Change your json response to object ( just remove[]
). 2. Change your parser to parse json array instead of object.