Android - Problems with managing server endpoints in a client application

I have the following server urls:

base
server url / method1
Base url server / method2 Base url server / method3
...
base url server / method100

I am currently using a properties file to store URLs. When I need to use some server urls, I read the properties file and executed the http request using Spring 4 Android

I know that this should be a different way (and a better way) to do this. What is the best practice to achieve this?

+4
source share
4 answers

- Plain Java:

public class ServerInfo {
     private static final String URL_BASE_DEBUG = "your base debug url";
     private static final String URL_BASE_RELEASE = "your base release url";

     public static String getBaseUrl() {
          //change if debug' release or whichever flavor
          return URL_BASE_DEBUG;
     } 
}

... Java Class

. ... ... Retrofit 2, okhttp

, Retrofit :

Rest Api (, User Rest api)

public interface SomeRestApiEndPoints {
    @GET("api/method1")
    Call<ReturnValue> method1(params);

    @POST(api/method2)
    Call<ReturnObject> method2(params);
    ...
}

Rest

public class RestClient {
   private static RestClient sRestClient;

   private Retrofit mRetrofit;
   private SomeRestApiEndPoints apiEndpoint;

   public static RestClient getRestClient () {
        if(sRestClient != null){
            return sRestClient;
        }

        synchronized (RestClient.class) {
            if(sRestClient == null) {
            //gson example
            Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
            RestClient client = new RestClient();
            client.mRetrofit = new Retrofit.Builder()
                    .baseUrl(ServerInfo.getBaseUrl()) //This is where you bind the base Url
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();     

            client.apiEndpoint = client.mRetrofit.create(SomeRestApiEndPoints.class);
            sRestClient = client;
        }
        return sRestClient;
    }


    public SomeRestApiEndPoints getApi() {
        return apiEndpoints;
    }

}

Call<ReturnValue> call = RestClient.getRestClient().getApi().method1(params);
+1

, android . Android Locale, buildtype flavors... URL- . urls.xml

    <resources>
        <string name="url_server1">http://apipas.com/gloabl/server1</string>
        <string name="url_server2">http://apipas.com/global/server2</string>
    </resources>

.. , , ( , /... : / : dev/testing/production), , .

, ... , build.gradle :

buildTypes {
    debug {
        debuggable false
        applicationIdSuffix 'debug'
    }
    qa {
        debuggable false
        applicationIdSuffix 'qa'
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

productFlavors {
    paid {
        applicationId = "com.apipas.android.wellmanagerurlsapp.paid"
    }
    free {
        applicationId = "com.apipas.android.wellmanagerurlsapp.free"
    }
}

de ru-rAU

, :

  • : / ( - : , ..)
  • BuildType: QA , .
  • : base, de en-rAU

, , res , ,

, buildType URLS:

enter image description here

Flavors:

enter image description here

buildType res.

URL-. , . /buildType/locale

JSON, XML , . , .

".

+3

, , - URL build.gradle buildType. , iosched.

, gradle.propeties :

# Website hostname
dev_website_host_name = devUrl.com 
production_website_host_name = productionUrl.com 

build.gradle , :

buildTypes {
    debug {
        debuggable true
        minifyEnabled false
        signingConfig signingConfigs.debug
        resValue("string", "website_host_name", "${dev_website_host_name}")
    }
    release {
        debuggable false
        minifyEnabled true
        // No signing config as we do this separately.
        proguardFiles getDefaultProguardFile('proguard-android.txt'), file('proguard-project.txt')
        resValue("string", "website_host_name", "${production_website_host_name}")
    }
}

website_host_name, . , R.string.website_host_name .

This is how I handle different types of assemblies in different environments. Google projects are always a good reference to keep in mind.

+1
source

The best way to do this is to use the REST API client. I would advise you to use Retrofit .

Here can be found here .

0
source

All Articles