简体   繁体   English

Uri.parse(),如何正确编码?

[英]Uri.parse(), how to get the encoding correct?

I am doing an application where I have to read a URL from a webpage as a String[Its not the address of the page]. 我正在做一个应用程序,在该应用程序中,我必须以字符串[不是页面的地址]的形式从网页中读取URL。 The URL that I will be reading contains query string, and I specifically need two queries from that URL. 我将要读取的URL包含查询字符串,并且我特别需要该URL中的两个查询。 So I am using the Uri class available in Android. 因此,我正在使用Android中提供的Uri类。 Now, the problem lies in the encoding/format of the URL and the query. 现在,问题出在URL和查询的编码/格式。 One of the queries that I need is always an URL. 我需要的查询之一始终是URL。 Sometimes the query URL is %-encoded and sometimes not. 有时查询URL是%编码的,有时不是。

The URLs can be like the following : URL可以如下所示:

Case 1 : 情况1 :

http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85 http://www.example.com/example/example.aspx?file=http%3A%2F%2FXX.XXX.XX.XXX%2FExample.file%3Ftoken%3D9dacfc85

Case 2 : 情况2:

http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85 http://www.example.com/example/example.aspx?file=http://XX.XXX.XX.XXX/Example.file?token=9dacfc85

How do I get the correct Url contained in the file= query? 如何获取file=查询中包含的正确网址?

I am using the following [to accomplish the said work universally] : 我正在使用以下[普遍完成上述工作]:

Uri.decode(urlString.getQueryParameter("file"));

Is this the correct way to do it? 这是正确的方法吗?


UPDATE 更新

I have decided to first encode the whole URL regardless of its value and then get the query parameter. 我决定先对整个URL进行编码,而不管其值如何,然后获取查询参数。 Theoretically, it should work. 从理论上讲,它应该起作用。

If you are uncertain about the type of URL you would get then I would suggest you to decode every URL you get from the parameter. 如果您不确定将获得的URL类型,那么我建议您对从参数获得的每个URL进行解码。 And when you need to use it then you can encode it. 并且当您需要使用它时,您可以对其进行编码。

As per my knowledge, you are doing it right. 据我所知,您做对了。

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

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