重定向链接后解码URL

时间:2013-08-02 03:23:39

标签: java android android-intent browser decode

我有一个不能编码的url1

String url1="http://bitake.com/params/?attribute=3"

此处url2(url1重定向后的url)被编码

String url2="http://search.bitake.com/params/?attr=48%e6%99%82%e9%96%93%e3%82"

我使用此链接打开含有Intent的浏览器

Intent browserIntent = new Intent("android.intent.action.VIEW",   
                                      Uri.parse(URLDecoder.decode(url));
startActivity(browserIntent);

我想通过代码从url1获取url2然后解码url2可以在web中打开 我该怎么办?

2 个答案:

答案 0 :(得分:0)

我认为你的url1不是一个有效的网址。无法通过utf8或GB

解码为有效网址

答案 1 :(得分:0)

您的URL1不是有效的网址。不推荐使用URLDecoder.decode(string url)。您应该使用URLDecode.decode(String url,String enc),其中enc是字符编码。

同样%必须编码为%25

public class DecodeTest
{
   public static void main(String[] args)
   {

      String url="http://bitake.com/params/?attribute=3";

      String url1="http://search.bitake.com/params/?attr=48%25e6%2599%2582%25e9%2596%2593%25e3%2582%25";

      try
      {
         System.out.println(URLDecoder.decode(url,"UTF-8"));
         System.out.println(URLDecoder.decode(url1, "UTF-8"));
      }
      catch (UnsupportedEncodingException e)
      {
         e.printStackTrace();
      }
   }
}