到 Uri 的 Android URL

我必须改变网址 URI,请指导我如何改变它。

我已经尝试了很多,但没有得到解决方案。感谢你的帮助。如果需要,我还可以附加代码片段。

98838 次浏览

We can parse any URL using Uri.parse(String) method.

Code Snippet :

Uri uri =  Uri.parse( "http://www.stackoverflow.com" );
final String myUrlStr = "xyz";
URL url;
Uri uri;
try {
url = new URL(myUrlStr);
uri = Uri.parse( url.toURI().toString() );
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}

This is my 2 Cents , it could help someone else.

The standard URL Parsing might not be good idea if you have variables passed to the URL as Query

String your_url_which_is_not_going_to_change = "http://stackoverflow.com/?q="
String your_query_string = "some other stuff"
String query = null;


try {
query = URLEncoder.encode(your_query_string, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


String url = your_url_which_is_not_going_to_change + query;


URL input = new URL(url);
etc... .

Hope it helps.

H.

var url = "http://10.0.2.2:8080/(mymobileapp)/login.php";

var response = await http.post(Uri.parse(url), body: data);