简体   繁体   English

在Android应用程序中存储api-urls的最佳方法是什么?

[英]What is the best way to store api-urls in android app?

I want to store the api urls somewhere , if I require I can change easily. 我想把api urls存储在某个地方,如果我需要我可以轻松改变。 If I keep in my server still I need url to access server which requires authentication. 如果我仍然在我的服务器中仍然需要url访问需要身份验证的服务器。 If I store in assets then there is a chance of stealing it by decompiling the apk. 如果我存储在资产中,则有可能通过反编译apk来窃取它。 I don't want to hardcode urls in code or String.xml. 我不想在代码或String.xml中硬编码URL。 Please suggest the best way where I can keep safe and change url easily. 请建议我可以保持安全并轻松更改网址的最佳方式。 Thanks !! 谢谢 !!

The best way in your case is store api url's in server because you can change them later. 在您的情况下,最好的方法是在服务器中存储api url,因为您可以稍后更改它们。 You'd create one url to fetch your api urls. 你要创建一个网址来获取你的api url。 Build your app based on an architecture like this. 根据这样的架构构建您的应用程序。

[
    {url: "/user/login", type:"login"},
    {url: "/user/register", type:"register"}
]

And don't forget anyone can see your server urls. 不要忘记任何人都可以看到你的服务器网址。 It even doesn't have to decompiled because there are tools to watch device's network traffic: 它甚至不需要反编译因为有工具来监视设备的网络流量:

make one Globalvariable.class in that you can add links. 制作一个Globalvariable.class ,你可以添加链接。

String link="http:/---";

and access them any where in project like 并在项目的任何地方访问它们

String url=Globalvairable.link;

simple. 简单。

Use google tagmanager for saving url in json format and use that in your app. 使用google tagmanager以json格式保存网址并在您的应用中使用它。 Its secure to save as it is encrypted and admin can change the url on the fly whenever needed. 它可以安全保存,因为它是加密的,管理员可以随时根据需要更改网址。

use buildConfigField in build.gradle :) 在build.gradle中使用buildConfigField :)

More info in https://developer.android.com/studio/build/gradle-tips?hl=es-419 有关详细信息, 请访问https://developer.android.com/studio/build/gradle-tips?hl=es-419

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM