1
\$\begingroup\$

In my application, I want to post the version name and manufacturer name to a server using retrofit. Are there any bad parts in the code below?

POJO:

public class InitPost {


    @SerializedName("appVersion")
    @Expose
    private String versionName;

    @SerializedName("deviceName")
    @Expose
    private String manufacturer;

     public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
}

Service interface:

public interface ApiService {

    @POST("postsInit")
    @FormUrlEncoded
    Call<InitPost> postInit(
           @Field("versionName") String versionName,
           @Field("deviceName") String manufacturer
 );
}

API Utils class:

public class ApiUtils {


  private static final String BASE_URL = "https://***.****.com/";

  private ApiUtils() {
  }

  public static ApiService getAPIService() {

    return RetrofitClient.getClient(BASE_URL).create(ApiService.class);
  }
}

Retrofit client:

class RetrofitClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient(String baseUrl) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }


}

Activity:

public class SplashActivity extends BaseActivity {

.....

String versionName;
String manufacturer;
private ApiService apiService;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        versionName = getVersionName();
        manufacturer = getManufacturer();

        apiService = ApiUtils.getAPIService();

        sendInit(versionName, manufacturer);


    }


private String getVersionName() {


        .....
        return versionName;

    }


private String getManufacturer() {

        manufacturer = Build.MANUFACTURER;
        Timber.d("MANUFACTURER: %s", manufacturer);
        return manufacturer;
    }


public void sendInit(String versionName, String manufacturer) {

        apiService.postInit(versionName, manufacturer).enqueue(new Callback<InitPost>() {
            @Override
            public void onResponse(@NonNull Call<InitPost> call, @NonNull Response<InitPost> response) {

                if (response.isSuccessful()) {
                    Timber.d("post submitted to API");
                    getInitResponse();
                }
            }

            @Override
            public void onFailure(@NonNull Call<InitPost> call, @NonNull Throwable t) {

                if (call.isCanceled()) {
                    Timber.e("Request was aborted");
                } else {
                    Timber.e("Unable to submit post to API.");
                }

            }
        });
    }




}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.