【问题标题】:How to Decode a URL using Java in Android? [duplicate]如何在 Android 中使用 Java 解码 URL? [复制]
【发布时间】:2014-02-06 09:49:03
【问题描述】:

我是 android 新手,我有这个 url 现在我需要编码这个 url:the url that has to be encoded。 参考使用 Java 在 Mobile 中下载 apk 我如何将此 url 传递到此行:

updateAppInstance.execute("http://demo.ingresssolutions.com/proposalmanagement/services/user/getApkFile");

Previous post Regarding Apk Download

http://demo.ingresssolutions.com/proposalmanagement/services/user/uploadFile

【问题讨论】:

    标签: java android parsing


    【解决方案1】:

    与您将阅读的许多答案相反,java.net.URLEncoder 不适用于对 URL 进行编码。它用于编码 URL 和 POST 参数。

    在 Java 中编码 URL 的正确方法如 this answer 中给出的,或者,如果您只需要对路径组件进行编码:

    String encodedPath = new URI(null, path, null).toASCIIString();
    

    【讨论】:

      【解决方案2】:
      import java.net.URL; 
      import java.net.URLEncoder;
      import java.net.MalformedURLException; 
      import java.io.UnsupportedEncodingException;
      
      public class Encoder{
       public static void main(String[] args){
        URL url = null; 
        try{
         url = new URL("http://www.stackoverflow.com");       
        }catch(MalformedURLException mue){
         System.err.println(mue); 
        }
        System.out.println(url + "
      "); 
        try{
         String encodedurl = URLEncoder.encode(url.toString(),"UTF-8"); 
         System.out.println(encodedurl);
        }catch(UnsupportedEncodingException uee){
         System.err.println(uee);
        }
       }
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2015-01-18
        • 2012-02-16
        • 2012-11-24
        • 2016-10-04
        • 2016-10-16
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多