How to create a android.net.Uri from a java.net.URL?

后端 未结 6 731
南方客
南方客 2020-12-15 04:21

I would like to use intent.setData(Uri uri) to pass data obtained from a URL. In order to do this, I need to be able to create a Uri from a URL (or from a byte

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 04:47

    I think your answer can be found from here..

    Uri.Builder.build() works quite well with normal URLs, but it fails with port number support.

    The easiest way that I discovered to make it support port numbers was to make it parse a given URL first then work with it.

    Uri.Builder b = Uri.parse("http://www.yoursite.com:12345").buildUpon();
    
    b.path("/path/to/something/");
    b.appendQueryParameter("arg1", String.valueOf(42));
    
    if (username != "") {
      b.appendQueryParameter("username", username);
    }
    
    String url = b.build().toString(); 
    

    Source : http://twigstechtips.blogspot.com/2011/01/android-create-url-using.html

提交回复
热议问题