Home > Web Front-end > HTML Tutorial > Decoding HTML format data in Andorid_html/css_WEB-ITnose

Decoding HTML format data in Andorid_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:39:54
Original
1433 people have browsed it

WebView is one of the commonly used components in Android development. It is used to load web page data. It can directly pass in the URL or Html format characters, etc. And we can process the loaded content through relevant methods in WebView, such as js interaction, obtaining the loaded web page link address information, etc. Today I encountered a small function during development, which is to use WebView to load a web page data. There are many products on the web page. Click on a certain product to enter the product details page in the App. The main method is through shouldOverrideUrlLoading in WebViewClient ( WebView view, String url) method to obtain url specific information and process it. For example, if I want to intercept part of the data in a specific character, the processing method is as follows:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
((CustomWebView) view).resetLoadedUrl( );
if(url != null ){
if(url.contains("?method=search¶meter=")){//For example, to determine whether the url includes the "?method=search¶meter=" character, Just intercept and get the text after "?method=search¶meter="

int length = "?method=search¶meter=".length();
int index = url.indexOf("?method=search¶meter= ");
url = URLDecoder.decode(url);//Data transcoding
String keyWord = url.substring(index length);//Interception operation
Bundle bundle = new Bundle();
bundle.putString("categoryName", keyWord);
mMainActivity.showActivity(activity, XX.class,bundle);//Transfer the intercepted data to the specified page
return true;

}else
//TODO
return true;
}
}
return false;
}

The above red color is not added at first Text, the intercepted characters were a long string of characters consisting of % and letters, etc., which were not the Chinese characters I wanted. Later, I added red text and got the characters I wanted, such as "Lenovo Computer".

Today I checked the java api and learned about the URLDecoder class:

A utility class for HTML format decoding. This class contains static methods for decoding a String from application/x-www-form-urlencoded MIME format.

This conversion process is exactly the opposite of the process used by the URLEncoder class. It is assumed that all characters in the encoded string are one of the following: "a" through "z", "A" through "Z", "0" through "9", and "-", "_", "." as well as"*". The "%" character is allowed, but is interpreted as the beginning of a special escape sequence.

The following rules are used in conversion:

  • Alphanumeric characters "a" to "z", "A" to "Z" and "0" to "9" Remain unchanged.
  • The special characters ".", "-", "*" and "_" remain unchanged.
  • The plus sign " " is converted to the space character " ".
  • will treat the "%xy" ​​format sequence as a byte, where xy is an 8-bit two-digit hexadecimal representation. All substrings containing one or more of these byte sequences consecutively are then replaced by characters whose encoding yields these consecutive bytes. The encoding mechanism for decoding these characters can be specified, or if not specified, the platform's default encoding mechanism is used.
  • This decoder has two possible ways of handling illegal strings. One way is to ignore the illegal character, the other way is to throw an IllegalArgumentException exception. The exact method used by the decoder depends on the implementation. The main method used now is:

    decode(String s, String enc) Use the specified encoding mechanism to decode the application/x-www-form-urlencoded string. Only after decoding can we get the correct data we want.

    The class similar to URLDecoder is URLEncoder: this class contains the conversion of String to application/x-www-form-urlencoded MIME format. Main method: encode(String s, String enc) Use the specified encoding mechanism to convert the string into application/x-www-form-urlencoded format.

    I will know how to solve this problem next time.

    Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template