Android-在应用程序客户端中管理服务器端点时出现问题
我有以下服务器URL: 基于服务器的url/method1Android-在应用程序客户端中管理服务器端点时出现问题,android,server,endpoint,spring-android,Android,Server,Endpoint,Spring Android,我有以下服务器URL: 基于服务器的url/method1 基于服务器的url/method2 基于服务器的url/method3 服务器基url/method100 我目前正在使用一个属性文件来存储URL。当我需要使用一些服务器URL时,我读取属性文件并使用Spring4Android执行http请求 我知道这应该是另一种方法(也是更好的方法)。实现这一点的最佳实践是什么?我肯定会选择android资源中的字符串值。所以您只需命名一次,就可以利用Android studio提供的强大工具,按
基于服务器的url/method2
基于服务器的url/method3
服务器基url/method100 我目前正在使用一个属性文件来存储URL。当我需要使用一些服务器URL时,我读取属性文件并使用Spring4Android执行http请求
我知道这应该是另一种方法(也是更好的方法)。实现这一点的最佳实践是什么?我肯定会选择android资源中的字符串值。所以您只需命名一次,就可以利用Android studio提供的强大工具,按区域设置、构建类型和风格进行定制和本地化。。。我只是建议在单独的文件中设置URL。i、 e.URL.xml
<resources>
<string name="url_server1">http://apipas.com/gloabl/server1</string>
<string name="url_server2">http://apipas.com/global/server2</string>
</resources>
它还支持de作为德语和Australianlocale en rAU
所以我们有这些定制因素:
- 味道:付费/免费(可以是其他东西:CPU拱门、区域等)
- BuildType:除了发布和调试之外,我还将QA添加到带有测试服务器的测试应用程序中(如果可用)
- 地区:基地、德鲁和恩劳
祝你好运。更好的方法是使用RESTAPI客户端。我建议你使用
可以找到一个正确的指南来了解如何使用它。另一个选择是使用纯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类中获得所需的所有信息
现在关于网络通信,这真的取决于你…有很多选择…我个人喜欢它是网络库顶部的包装
关于如何使用上述方法进行改装
的简短示例如下:
restapi(与用户restapi类似)
用法示例
Call<ReturnValue> call = RestClient.getRestClient().getApi().method1(params);
Call Call=RestClient.getRestClient().getApi().method1(params);
我要做的是根据构建类型在build.gradle
文件中创建URL参数。这是伟大项目所遵循的战略
首先要做的是将gradle.properties
文件中的行添加到文件中:
# 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将创建名为网站\u主机\u名称
的字符串资源,并且根据构建类型将有一个或另一个值。要通过代码访问该值,只需键入R.string.website\u host\u name
,即可
这就是我在不同环境中处理不同构建类型的方式。谷歌的项目始终是一个值得铭记的好参考。你已经满足了我的所有要求。关于如何使用这个库,您给出了一个很好的示例。我想这正是我想要的。谢谢你的回答。你赢得了赏金,我正在讨论向资产文件夹添加属性文件,并解析该文件。我认为这种方法更干净
Call<ReturnValue> call = RestClient.getRestClient().getApi().method1(params);
# Website hostname
dev_website_host_name = devUrl.com
production_website_host_name = productionUrl.com
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}")
}
}