URI,URL
- URI=Uniform Resource Identifier: uniform resource identifier, which can uniquely identify a resource under a certain rule. For example, your ID number.
- URL=Uniform Resource Locator: uniform resource locator, which can provide the path to find the resource. It is a subset of URIs and a URI implemented by location.
On www, every information resource has a unified and unique address on the Internet. This address is called URL, which refers to the network address.
2, Composition of URL
The general syntax format of URL is (those with square brackets [] are optional)
protocol://hostname[:port]/path/[;parameters][?query]#fragment agreement://Domain name: port number / directory / file name File suffix? Parameter = value # flag https://www.testurl.com:8080/path/subpath;parms=test_parms?uid=123&gid=45#fragment=009&fragment
- Protocol protocol. The commonly used protocol is http
- hostname host address, which can be domain name or IP address
- Port the default port of http protocol is port 80. If it is not written, the default port is port 80
- Path path the specified path of the network resource in the server
- Parameter parameter if you want to pass a parameter to the server, enter it in this section
- Query query string if you need to query the content from the server, edit it here
- Fragment fragment the web page may be divided into different fragments. If you want to access the web page and directly reach the specified location, you can set it in this part
Scheme
English meaning: scheme, format and strategy
It is a custom protocol, non-standard. In many cases, scheme is the name of the protocol, which defines how to obtain resources.
Attention
- Encoding, query should be encoded, otherwise the direct parsing using the system method will fail
give an example
val url = "taobao://app/path?version=1.0&name=zhaoyanjun&open=true" val uri = Uri.parse(url) val scheme = uri.scheme val host = uri.host val path = uri.path val version = uri.getQueryParameter("version") val name = uri.getQueryParameter("name") val open = uri.getBooleanQueryParameter("open", false) Log.d("yu--", "scheme:$scheme") Log.d("yu--", "host:$host") Log.d("yu--", "path:$path") Log.d("yu--", "version:$version name:$name") Log.d("yu--", "open:$open")
journal:
D/yu--: scheme:taobao D/yu--: host:app D/yu--: path:/path D/yu--: version:1.0 name:zhaoyanjun D/yu--: open:true
encode
Online encode: http://www.jsons.cn/urlencode/
query should be encoded, otherwise direct parsing using system methods will fail.
Why code? For example, I have two parameters:
- Parameter 1: H5= https://baidu.com/step?_bid=11&version=1
- Parameter 2: version=1234
The complete scheme is as follows:
taobao://app/path?h5=https://baidu.com/step?_bid=11&version=1&&version=1234
Next, get the parameters h5 and version respectively
val h5 = uri.getQueryParameter("h5") val version = uri.getQueryParameter("version") result: h5:https://baidu.com/step?_bid=11 version:1
Obviously, the obtained value is not the correct value we want. Let's encode the whole query parameter to see the effect. The encoded scheme is as follows:
Before parameter 1 Coding: https://baidu.com/step?_bid=11&version=1 After parameter 1 Coding: https%3A%2F%2Fbaidu.com%2Fstep%3F_bid%3D11%26version%3D1 Parameter 2 before coding: 1234 Parameter 2 after coding: 1234
Combine the two parameters, and the complete scheme is as follows:
taobao://app/path?h5=https%3A%2F%2Fbaidu.com%2Fstep%3F_bid%3D11%26version%3D1&version=1234
The relevant parameters of post removal are as follows:
D/yu--: scheme:taobao D/yu--: host:app D/yu--: path:/path D/yu--: h5:https://baidu.com/step?_bid=11&version=1 D/yu--: version:1234
h5 how to evoke app through scheme
Add scheme value in Activity
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- scheme start --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.APP_BROWSER" /> <data android:host="app" android:path="/path" android:scheme="taobao" /> </intent-filter> <!-- scheme end --> </activity>
WEB end through call taobao://app/path?query1=1&query2=true You can open this Activity. scheme and host are required. In addition, it depends on the requirements.
Open via WEB
<!DOCTYPE html> <html> <head> <title>test</title> </head> <body> <a href="taobao://app/path? Query1 = 1 & query2 = true "> Open app</a> </body> <html>
The core is the URL of a Schema protocol. scheme and host are necessary to open the APP page. The parameters passed can be obtained in the APP page.
Open through another APP
val intent = Intent( Intent.ACTION_VIEW, Uri.parse("taobao://app/path?query1=1&query2=true") ) startActivity(intent)
Get scheme value
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val uri = intent.data if (uri != null) { val scheme = uri.scheme val host = uri.host val path = uri.path val query1 = uri.getQueryParameter("query1") val query2 = uri.getQueryParameter("query2") Log.d("yu--", "scheme:$scheme") Log.d("yu--", "host:$host") Log.d("yu--", "path:$path") Log.d("yu--", "query1:$query1") Log.d("yu--", "query2:$query2") } } }