Android - 在应用程序客户端中管理服务器端点时遇到问题

Android - Having problems managing server endpoints in app client

我有以下服务器网址:

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

我目前正在使用属性文件来存储 URL。当我需要使用一些服务器 URL 时,我会读取属性文件并使用 Spring 4 Android

执行 http 请求

我知道这应该是另一种方式(也是一种更好的方式)来做到这一点。实现此目标的最佳做法是什么?

我肯定会使用值作为字符串的 android 资源。所以你只要给它命名一次,就可以利用来自 Android studio 的强大工具,通过 Locale、buildtype 和 flavors 进行定制和本地化……我只是建议在单独的文件中设置 urls。即 urls.xml

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

对于简单的项目,这可能无关紧要。但是当您处理企业项目或多个目标、多语言、不同服务器层(基于位置;EU/Asia...,或使用情况:free/paid,或阶段:dev/testing/production) 您 select 管理资源等方式的方式至关重要。

让我详细说明...假设我们有这个 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 作为德语和 澳大利亚 语言环境 en-rAU

所以我们有这些定制因素:

  • 风味:paid/free(它可以是其他东西:CPU arch,regions,...等)
  • BuildType:除了 Release 和 Debug 之外,我还添加了 QA 以使用测试服务器测试应用程序(如果可用)。
  • 语言环境:base、de 和 en-rAU

您需要做的就是在正确的位置覆盖 res 来满足您的需要

假设我要使用 buildType 基础来自定义 URLS:

或者你会使用口味:

注意flavor和buildType一起使用自定义res时不要重复掉

在开发过程中,你不需要更改url的名称。当你有大项目时,这真的是一个好点。您仍然可以轻松更改任何 flavor/buildType/locale

的内容

您还可以使用 JSON、XML 或您提到的属性。但是 none 他们会给你什么 res 做。

祝你好运,'。

更好的方法是使用 REST API 客户端。我建议您使用 Retrofit.

可以找到了解如何使用它的正确指南 here

另一种选择是使用 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 which is a wrapper on top of a networking library okhttp

关于如何将 Retrofit 与上述方法结合使用的一个简短示例是:

休息Api(如用户休息api)

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

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

其余客户

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);

我用来做的是根据 buildType 在 build.gradle 文件中创建 URL 参数。这是伟大的 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}")
    }
}

Grade 将创建名为 website_host_name 的字符串资源,并且根据您的构建类型将具有一个或另一个值。要通过代码访问该值,只需键入 R.string.website_host_name 即可!

这就是我在不同环境下处理不同构建类型的方式。 Google 的项目始终是值得牢记的好参考。