how to url encode in android?

前端 未结 8 742
盖世英雄少女心
盖世英雄少女心 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.

提交回复
热议问题