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

Jun 24, 2016 am 11:39 AM

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.

    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    See all articles