how to url encode in android?

前端 未结 8 715
盖世英雄少女心
盖世英雄少女心 2020-12-05 10:59

I am using grid view for displaying image using xml parsing,i got some exception like

java.lang.IllegalArgumentException: Illegal character in path a

相关标签:
8条回答
  • 2020-12-05 11:46

    I tried with URLEncoder that added (+) sign in replace of (" "), but it was not working and getting 404 url not found error.

    Then i googled for get better answer and found this and its working awesome.

    String urlStr = "http://www.example.com/test/file name.mp4";
    URL url = new URL(urlStr);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
    url = uri.toURL();
    

    This way of encoding url its very useful because using of URL we can separate url into different part. So, there is no need to perform any string operation.

    Then second URI class, this approach takes advantage of the URI class feature of properly escaping components when you construct a URI via components rather than from a single string.

    0 讨论(0)
  • 2020-12-05 11:48

    URL encoding is done in the same way on android as in Java SE;

    try {
        String url = "http://www.example.com/?id=123&art=abc";
        String encodedurl = URLEncoder.encode(url,"UTF-8");
        Log.d("TEST", encodedurl);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } 
    
    0 讨论(0)
提交回复
热议问题