【问题标题】:How can you add to network_security_config from MainActivity如何从 MainActivity 添加到 network_security_config
【发布时间】:2019-07-17 12:00:06
【问题描述】:

我有一个应用程序,我通过 OkHTTP 从网络服务器获取一些值,该应用程序可以正常工作并将其显示在 MainActivity 上。现在我想将这些收到的值添加到network_security_config.xml

为简单起见,我在 MainActivity 中设置了一个字符串等于YZPgTZ+woNCCCIW3LH2CxQeLzB/1m42QcCTBSdgayjs=

现在我希望这个字符串出现在我的network_security_config.xmlVALUE_I_WANT_TO_ADD。我该怎么做呢?我可以为此使用 jdom 吗?

network_security_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">google.com</domain>
      <pin-set expiration="2020-01-01">
  <pin digest="SHA-256">MbZtXtN6X71CNe/UJzKFH0UGnPWGux5/zo5BRaJpkvI=</pin>

<pin digest="SHA-256">VALUE_I_WANT_TO_ADD</pin>
      </pin-set>
  </domain-config>
</network-security-config>

【问题讨论】:

  • 不能在运行时修改网络安全配置。
  • Manifest 是不可变的,我认为,你不能在运行时更改它
  • @CommonsWare 我知道 res 中的 xmls 无法更改,但是我该如何添加/更新一些东西呢?
  • 你不能在运行时修改资源文件夹中的任何文件,你想保存一些东西以备后用吗?
  • "但是我怎样才能添加/更新一些东西呢?" ——你不能。没有 API 可供您更改网络安全配置,无论是文件形式还是该数据的内存中表示形式。

标签: java android xml


【解决方案1】:

从您的问题看来,您希望保存字符串以备后用,因此您可以使用SharedPreferences 保存String 以备后用。

final String TAG = "some final text";
sharedPref = new SharedPreferences();
sharedPref = getSharedPreferences(TAG,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString("key","your_string");
    editor.apply();

稍后,当您再次运行您的应用程序时,您可以从SharedPreferences 检索您的字符串,如下所示

    String value = sharedPref.getString("key","default_value");

【讨论】:

  • 供以后使用,如何添加到xml中?
  • 你的 xml 文件在哪里?
  • xml文件位于Androidwebviewer\app\src\main\res\xml\network_security_config.xml
  • 您不能在应用运行时以任何方式修改android应用源文件结构的任何文件。你必须转向任何其他方法来解决你的问题。
  • 干杯,我会尝试另一种方法来代替这个。感谢您的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多