简体   繁体   English

在JAVA中从URL获取资源xml文件

[英]Get resources xml file from url in JAVA

Currently I am storing my resources files on local directory and I am calling them by: 当前,我将资源文件存储在本地目录中,并通过以下方式调用它们:

Resources res = getResources();
    final String[] docukrz = res.getStringArray(R.array.docukrz);

I wanted to move this online, so I have created a url: https://example/strings.xml 我想将其在线移动,所以我创建了一个URL: https://example/strings.xml

Now how I can get that file so I will call it from URL, not resources folder. 现在,我将如何获取该文件,以便从URL而非资源文件夹中调用它。

I am a total beginner and read a lot of different articles and tutorials but cannot pull that together. 我是一个完全的初学者,阅读了很多不同的文章和教程,但无法综合考虑。 Sorry if that is a duplicate. 抱歉,如果重复的话。

That is not how resources work. 这不是资源的工作方式。 The resources framework is built around local storage, a specific set of directories and "qualifiers", and runtime determination of which resources should be available from the app. 资源框架围绕本地存储,一组特定的目录和“限定符”以及运行时确定应从应用程序中获取哪些资源而构建。

You are welcome to download an xml file from the internet and parse it, but this will never be part of your app's resources. 欢迎您从互联网上下载xml文件并进行解析,但这绝不会成为您应用程序资源的一部分。

I can suggest you use Retrofit and Kripton (I'm the author of this library) libraries to download an XML file from a URL and convert it into a Java object (or objects). 我可以建议您使用RetrofitKripton (我是该库的作者)库从URL下载XML文件并将其转换为Java对象。

The wiki page of Kripton tries to explain how to do: instead of use JSON parser, you need to use XMLParser: Kripton的Wiki页面试图解释如何做:您需要使用XMLParser而不是JSON解析器:

// create retrofit using Kripton converter factory
Retrofit retrofit = new Retrofit.Builder()
  .baseUrl(“https://jsonplaceholder.typicode.com/")
  .addConverterFactory(KriptonBinderConverterFactory.create(BinderType.XML))
  .build();
JsonPlaceHolderService service = Retrofit.create(JsonPlaceHolderService.class);
// consume service
Response<List<Post>> response = service.getAllPost().execute();

More information about Retrofit and Kripton Persistence Library: 有关翻新和Kripton持久性库的更多信息:

If you want to see a working example using Retrofit and Kripton Persistence Library, you can see my example app RSS Reader on GitHub . 如果您想使用Retrofit和Kripton Persistence Library来查看工作示例,则可以在GitHub上查看我的示例应用程序RSS Reader。

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

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