【问题标题】:URLEncoder encode / URLDecoder decode in java (Android)java (Android) 中的 URLEncoder 编码 / URLDecoder 解码
【发布时间】:2010-10-28 03:48:05
【问题描述】:

我想在应用程序中使用 URLEncoder/URLDecoder 类 (java.net.URLEncoder/URLDecoder) 和方法:encode(String s, String enc)/decode(String s, String enc),但我没有不知道字符串参数 enc 的值是多少? 我想在“x-www-form-urlencoded”MIME 内容类型中编码/解码。 感谢您的帮助。

【问题讨论】:

    标签: java android


    【解决方案1】:

    编码参数是您使用的字符编码。 For example "UTF-8".

    【讨论】:

    • 所以我必须这样写: encode("hello","x-www-form-urlencoded") 或者对于 "x-www-form-urlencoded" 它是 encode("hello" "UTF-8")?
    • 你必须写 encode("hello","UTF-8")。 x-www-form-urlencoded 始终是结果的类型。编码参数是指您的输入数据。
    • 是否有预定义的常量用于可能的字符编码?
    • android 文档太糟糕了! URLEncoder 类文档甚至没有提到“UTF-8”developer.android.com/reference/java/net/URLEncoder.html
    【解决方案2】:

    首先,您需要将内容类型设置为“x-www-form-urlencoded”。然后,无论您想编码什么内容,都使用“UTF-8”对其进行编码。

    例如:

    将内容设置为“x-www-form-urlencoded”:

    URL url = new URL("http://www.xyz.com/SomeContext/SomeAction"); <br>
    URLConnection urlConnection = url.openConnection();<br>
    ....<br>
    ....<br>
    urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    


    或者,如果您正在使用一些 JSP,那么您可以在其上编写以下内容。

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br>
    < META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    
    < FORM action="someaction.jsp" enctype="application/x-www-form-urlencoded" name="InputForm" method="POST">
    

    以及使用 URLEncoder:

    String encodedString = URLEncoder.encode("hello","UTF-8");
    

    【讨论】:

    • 我已经完成了: - HttpPost post = new HttpPost("url"); - post.setHeader("Content-Type", "application/x-www-form-urlencoded"); - 字符串编码字符串 = URLEncoder.encode("hello","UTF-8"); - 字符串实体临时; - temp.setContentEncoding(encodedString); - post.setEntity(temp);然后执行一个 DefaultHttpClient 和 post 作为参数。
    • 问题实际上是由于我在 encode(String s, String enc) 中放置了“application/x-www-form-urlencoded”而不是“UTF-8”...再次谢谢。
    【解决方案3】:

    JavaDoc 包含所有详细信息

    【讨论】:

    • JavaDoc 没有提到所有可能的编码格式,我也没有看到指向可能列出它们的 ENUM 的链接。
    【解决方案4】:

    URLEncoder 和 URLDecoder 都是异常 Throwable,因此必须至少包含在 try/catch 块中。但是使用android.net.Uri 有一点更简单的方法:

    Uri.decode(string);
    Uri.encode(string);
    

    这些是静态方法,使用 utf-8,从 API-1 开始可用并且不会引发异常。

    【讨论】:

      【解决方案5】:

      我个人最喜欢的:

      static String baseNameFromURL(URL url) {
          String shortName;
          String path = url.getPath();
          String escaped = path.substring(path.lastIndexOf('/') + 1);
          try {
              shortName = URLDecoder.decode(escaped, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              throw new Error(e.getMessage(), e);
          }
          int period = shortName.lastIndexOf('.');
          return period > -1 ? shortName.substring(0, period) : shortName;
      }
      

      如果 URL 没有文件名部分,则返回一个空字符串,例如 https://stackoverflow.com/ 或 https://stackoverflow.com/questions/。如果文件名部分有反斜杠,则保留。

      如果您需要带有扩展名的短名称,则可以去掉最后两行。

      【讨论】:

        猜你喜欢
        • 2012-12-28
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多