java 对包含 JSONArray 的 URL 进行编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3792124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encode a URL Containing a JSONArray
提问by wtollett
I've got an android application that I'm attempting to use to pass some data to a webservice using HTTPGet. If I just construct the string using the JSONArray.toString() method, I end up with a URL that looks something like the following:
我有一个 android 应用程序,我试图用它来使用 HTTPGet 将一些数据传递给 web 服务。如果我只是使用 JSONArray.toString() 方法构造字符串,我最终会得到一个类似于以下内容的 URL:
http://xxx.xx.xxx.xx/api?method=upload&args[deviceID]=123456789&args[data]=["{element1=93295352, element2=235235, element3=3523523}","{element1=93295352, element2=235235, element3=3523523}"]
This doesn't work because of the spaces and quotation marks in the URL. If I attampt to do something like the following:
由于 URL 中的空格和引号,这不起作用。如果我尝试执行以下操作:
JSONArray ja = new JSONArray();
// Add Data to JSONArray
String s = ja.toString();
// Add array to StringBuilder url
HTTPGet httpget = new HTTPGet(UrlEncoder.encode(url.toString()));
I get an error thrown because the entire URL gets encoded and ends up like this:
我收到一个错误,因为整个 URL 被编码并以这样的方式结束:
http%3A%2F%2Fxxx.xx.xxx.xx%2Fapi%3Fmethod%3Dupload%26args%5BdeviceID%5D%3D123456879%26args%5Bdata%5D%3D%5B%22%7Belement1%3D915156028000%2C+element2%3D1651651%2C+element3%3D489461%7D%22%2C%22
Obviously, this isn't what I'm looking for, and there's got to be a better solution than searching/replacing all the necessary characters in the JSONArray portion of that url, though I suppose doing it that way wouldn't be a huge hassle since I'm only worried about quotation and space characters.
显然,这不是我正在寻找的,并且必须有比搜索/替换该 url 的 JSONArray 部分中的所有必要字符更好的解决方案,尽管我认为这样做不会是一个巨大的麻烦,因为我只担心引号和空格字符。
Note that manually pasting this into my browser returned the results I expect:
请注意,手动将其粘贴到我的浏览器中会返回我期望的结果:
http://xxx.xx.xxx.xx/api?method=upload&args[deviceID]=123456789&args[data]=[%22{element1=915156028000,%20element2=0.0,%20element3=2.297444}%22,%22{element1=915156055000,%20element2=0.0,%20element3=2.2974419999999998}%22]
回答by Stephen C
You need to URL encode justthe query argument value; for example
你需要URL编码只查询参数值; 例如
JSONArray ja = new JSONArray();
// Add Data to JSONArray
String s = UrlEncoder.encode(ja.toString());
// Add string to StringBuilder url
HTTPGet httpget = new HTTPGet(url.toString());
The other thing that you need to be aware of is that there are practical limits on the length of a URL. These limits vary from one implementation to another (browser, server, proxy, HTTP client library, etc). In some cases, it is as low as 2k bytes, IIRC.
您需要注意的另一件事是 URL 的长度存在实际限制。这些限制因一种实现而异(浏览器、服务器、代理、HTTP 客户端库等)。在某些情况下,它低至 2k 字节,IIRC。
FOLLOW UP
跟进
If I want to do the same thing using POST instead, do I end up having to encode the data in the same manner?
如果我想使用 POST 做同样的事情,我最终是否必须以相同的方式对数据进行编码?
It depends.
这取决于。
- If you sent the arguments as URL query arguments with a POST, the same encoding rules and limits would apply. For the record, the URL encoding rules are part of the URLand URIspecs.
- If instead you sent the arguments as POST data using the "application/x-www-form-urlencoded" content type, the encoding rules are a bit different; see the HTML spec. (The main difference is that this encoding encodes spaces as
+
characters.) - You could also encode the POST data in some other way, provided that the HTTP client and server both understood the encoding and content type.
- 如果您使用 POST 将参数作为 URL 查询参数发送,则将应用相同的编码规则和限制。作为记录,URL 编码规则是URL和URI规范的一部分。
- 相反,如果您使用“application/x-www-form-urlencoded”内容类型将参数作为 POST 数据发送,则编码规则会有所不同;请参阅HTML 规范。(主要区别在于这种编码将空格编码为
+
字符。) - 您还可以以其他方式对 POST 数据进行编码,前提是 HTTP 客户端和服务器都理解编码和内容类型。
Of course, one of the advantages of using POST data is that there is typically no limit on the size of the arguments you can send.
当然,使用 POST 数据的优点之一是您可以发送的参数大小通常没有限制。