Home  >  Article  >  Backend Development  >  android cannot parse json data of php

android cannot parse json data of php

王林
王林Original
2019-10-21 13:47:002465browse

android cannot parse json data of php

Error reason:

BOM header problem. The server side is PHP, and the returned transfer data '{' is preceded by a BOM header. When Java reads the header, it reads the header directly as the file content, and then an error occurs.

What is the BOM header?

The BOM header is also called UTF-8 signature. In fact, the UTF-8 BOM has no effect on UFT-8. It is a BOM added to support UTF-16 and UTF-32.

The meaning of BOM signature is to tell the editor which encoding the current file uses. However, although the BOM is not displayed in the editor, it will produce output, just like an extra blank line. It is a string of hidden characters used to let editors such as Notepad identify whether the file is encoded in UTF-8.

Solution:

java, android client, etc. can filter out the BOM through code and then parse it.

/**
 * 去除bom报头
 */
public static String formatString(String s) {
    if (s != null) {
          s = s.replaceAll("\ufeff", "");
    }
    return s;
}

Recommended tutorial: PHP video tutorial

The above is the detailed content of android cannot parse json data of php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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