Response from retrofit2 GET request returns wrong data but I don't know why

I have an android app that fetches data from a database using an access token. I have successfully setup the oAuth2 side of the app and can login with credentials and store the access token to be used with GET requests. Now I am trying to fetch some customer data from the database but the response returns the package name and not the customer data.

Below you can find my classes:

package com.example.helper.retrofit.oauth2.response;  public class CustomerResponse {      private Integer id;     private String name;     private String tel;     private String email;     private String address1;     private String address2;     private String address3;     private String town;     private String county;     private String postcode;       public Integer getId() {         return id;     }      public void setId(Integer id) { = id;     }      public String getName() {         return name;     }      public void setName(String name) { = name;     }      public String getTel() {         return tel;     }      public void setTel(String tel) { = tel;     }      public String getEmail() {         return email;     }      public void setEmail(String email) { = email;     }      public String getAddress1() {         return address1;     }      public void setAddress1(String address1) {         this.address1 = address1;     }      public String getAddress2() {         return address2;     }      public void setAddress2(String address2) {         this.address2 = address2;     }      public String getAddress3() {         return address3;     }      public void setAddress3(String address3) {         this.address3 = address3;     }      public String getTown() {         return town;     }      public void setTown(String town) { = town;     }      public String getCounty() {         return county;     }      public void setCounty(String county) {         this.county = county;     }      public String getPostcode() {         return postcode;     }      public void setPostcode(String postcode) {         this.postcode = postcode;     } }

package com.example.packagename;  import;  import android.os.Bundle; import android.widget.Toast;  import com.example.helper.retrofit.oauth2.OauthConstant; import com.example.helper.retrofit.oauth2.response.CustomerResponse; import com.example.helper.retrofit.oauth2.service.CustomerService;  import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory;    public class ViewCustomers extends AppCompatActivity {       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_view_customers);          Retrofit retrofit = new Retrofit.Builder()                 .baseUrl(OauthConstant.DATA_SERVER_URL)                 .addConverterFactory(GsonConverterFactory.create())                 .build();          CustomerService customerService = retrofit.create(CustomerService.class);          Call<CustomerResponse> call = customerService.getCustomers();         call.enqueue(new Callback<CustomerResponse>() {             @Override             public void onResponse(Call<CustomerResponse> call, retrofit2.Response<CustomerResponse> response) {                 if (response.isSuccessful()) {                     Toast.makeText(ViewCustomers.this, response.body().toString(), Toast.LENGTH_LONG).show();                 } else {                     Toast.makeText(ViewCustomers.this, "Not successful!", Toast.LENGTH_LONG).show();                 }              }              @Override             public void onFailure(Call<CustomerResponse> call, Throwable t) {                 Toast.makeText(ViewCustomers.this, "Failed!", Toast.LENGTH_LONG).show();             }         });      } }

package com.example.packagename.helper.retrofit.oauth2.client;  import android.content.Context; import android.content.SharedPreferences;  import;  import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okio.Buffer;  import static com.example.MainActivity.access; import static com.example.MainActivity.mypreference;  public class cCustomerService {      private Context mContext;     SharedPreferences sharedPreferences;      OkHttpClient client = new OkHttpClient.Builder()             .addInterceptor(new Interceptor() {                  @Override                 public Response intercept(Chain chain) throws IOException {                     Request request = chain.request();                     Request.Builder requestBuilder = request.newBuilder();                     mContext.getSharedPreferences(mypreference, Context.MODE_PRIVATE);                      RequestBody formBody = new FormBody.Builder()                             .add("access_token", sharedPreferences.getString(access, ""))                             .build();                      String postBodyToString = bodyToString(request.body());                     postBodyToString += ((postBodyToString.length() > 0) ? "&" : "") + bodyToString(formBody);                     request ="application/x-www-form-urlencoded;charset=UTF-8"), postBodyToString))                             .build();                      return chain.proceed(request);                 }             }).build();       public static String bodyToString(final RequestBody requestBody) {         try {             final RequestBody copy = requestBody;             final Buffer buffer = new Buffer();             if (copy != null) {                 copy.writeTo(buffer);             } else {                 return "";             }             return buffer.readUtf8();         } catch (IOException e) {             return "didn't work";         }     }  }

package com.example.helper.retrofit.oauth2.service;  import com.example.helper.retrofit.oauth2.response.CustomerResponse;  import retrofit2.Call; import retrofit2.http.GET;  public interface CustomerService {      @GET("customer/")     Call<CustomerResponse> getCustomers(); } 

The value returned is com.examp[email protected]a6bc5ca each time I run the request it returns some different digits at the end, I’m not sure if this has any relevance.

Is there something I’m doing wrong?

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.