0

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:

JSON

I'm getting the following error message:

image

3
  • 1
    Possible duplicate of GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"? Commented Apr 10, 2018 at 5:12
  • post json as code not as image
    – Vidhi Dave
    Commented Apr 10, 2018 at 5:15
  • You are trying to parse json response as object where you json response contains an array ( starts and ends with [] ). 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. Commented Apr 10, 2018 at 5:31

2 Answers 2

2

Use List Because its array and parse the array of objecct:

public interface ApiService {
@FormUrlEncoded
@GET("/lara/getprofile.php?id=")
Call<List<Profile>> getMyProfile(@Query("id") String id);
}

Instead:

public interface ApiService {
@FormUrlEncoded
@GET("/lara/getprofile.php?id=")
Call<Profile> getMyProfile(@Query("id") String id);
}
4
  • Thanks @R2R! I'm almost there. Not sure how to display to the TextViews. @Override public void onResponse(Call<List<Profile>> call, Response<List<Profile>> response) { Profile p = response.body(); ---Getting error on this line, incompatible types. Found java.util.List psid.setText(p.getPsid()); fname.setText(p.getFname()); lname.setText(p.getLname()); teamlead.setText(p.getTeamlead()); }
    – Kent
    Commented Apr 10, 2018 at 5:53
  • you have retrive like array...response.get(0).getPsid();....0 is the index ArrayList<Profiel> profileList=response.body(); if its helpful agree the answer Commented Apr 10, 2018 at 5:55
  • I'm sorry, not sure what I'm doing. After updating the Apiservice, I got errors on my callback method so I tried to play with it by setting the value of one of the textviews but nothing's happening when running the app. Here's an update on my callback method:
    – Kent
    Commented Apr 10, 2018 at 22:48
  • private void getProfile(final String id){ ApiService apiService = ApiClient.getClient().create(ApiService.class); Call<List<Profile>> call = apiService.getMyProfile(id); call.enqueue(new Callback<List<Profile>>() { @Override public void onResponse(Call<List<Profile>> call, Response<List<Profile>>response) { psid.setText(id); }
    – Kent
    Commented Apr 10, 2018 at 22:48
1

You are doing wrong because you are parsing your response As Object for Profile but actually, a response in the array means you need to parse response as an array, replace your API service as bellow also change calling as bellow

public interface ApiService {
    @FormUrlEncoded
    @GET("/lara/getprofile.php?id=")
    Call<List<Profile>> getMyProfile(@Query("id") String id);
}



private void getProfile(final String id){
  ApiService apiService = ApiClient.getClient().create(ApiService.class);
    Call<List<Profile>> call = apiService.getMyProfile(id);
    call.enqueue(new Callback<List<Profile>>() {
        @Override
        public void onResponse(Call<List<Profile>> call, Response<List<Profile>> response) {

            progressDialog.dismiss();
            List<Profile> p = response.body();
            if(p!=null && p.size()>0){
            pid.setText(p.get(0).getPid());
            fname.setText(p.get(0).getFname());
            lname.setText(p.get(0).getLname());
            teamlead.setText(p.get(0).getTeamlead());}
        }

        @Override
        public void onFailure(Call<List<Profile>> call, Throwable t) {
            //progressDialog.dismiss();
            Toast.makeText(ProfilePage.this, "Failed to load." + t, Toast.LENGTH_LONG).show();
    }
    });
}
4
  • Thanks! Here's what I did to the callback method so far but I'm not able to figure out what's wrong. private void getProfile(final String id){ ApiService apiService = ApiClient.getClient().create(ApiService.class); Call<List<Profile>> call = apiService.getMyProfile(id); call.enqueue(new Callback<List<Profile>>() { @Override public void onResponse(Call<List<Profile>> call, Response<List<Profile>>response) { }
    – Kent
    Commented Apr 10, 2018 at 23:33
  • The line Profile p = response.body(); is the only one that's having an error now. Message: Incompatible types, found: java.util.list
    – Kent
    Commented Apr 11, 2018 at 5:10
  • I got it @Dhaval by using the following code: List<Profile> person = response.body(); progressDialog.dismiss(); // Can iterate through list and grab Getters from POJO for(Profile p: person){ fname.setText(p.getFname().toString()); lname.setText(p.getLname().toString()); }
    – Kent
    Commented Apr 11, 2018 at 6:03
  • Thanks for the help!
    – Kent
    Commented Apr 11, 2018 at 6:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.