1

我正在尝试使用 PHP 传入数据(来自服务器),并且数据由汉字组成。我正在尝试使用我确实设法检索数据的 JSON 解析器解析数据,但数据(中文)显示为???. 有人可以告诉我出了什么问题吗?

下面是我的解析器:

// function get json from url
    // by making HTTP POST or GET method
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

正如在我的 logcat 上看到的那样。

11-09 07:18:53.939: D/Single Product Details(570): {"success":1,"search":[{"tel1":"67546498","generalEmail":"anderlab01@yahoo.com.sg","contactEmail":"jeff1285@hotmail.com","lineOfBusiness":"??????????? Manufacture,Import\/Export","postCode1":"738733","companyNameEng":" ANDER LAB PTE LTD ","repName1Eng":"Li Jianfu","address1":"Blk 20 Woodlands Link #04-40","fax1":"67544092","repName1Chi":"???","url":"www.bosun.com.sg"}]}
4

4 回答 4

2

我的数据库已经默认为utf8,遇到同样的问题。

添加通常的元标记“”后,问题仍然存在。

然后我创建了一个专用的connection.php来确保与mysql的所有通信都是charset utf8,注意mysqli_set_charset($bd, 'utf8')中的utf8中没有“-” !

连接.php

<?php
    $mysql_hostname = "localhost";
    $mysql_user = "username";
    $mysql_password = "password";
    $mysql_database = "dbname";
    $prefix = "";
    $bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
    mysqli_select_db($bd, $mysql_database) or die("Could not select database");
    if(!mysqli_set_charset($bd, 'utf8'))  {
        exit() ;
    }

?>

这是发送检索数据的示例。

<?php

    //Include database connection details
    require_once('connection.php');

        enter code here

    //Create query
    $qry = "SELECT * FROM subject";
    $result = mysqli_query($bd, $qry);
    //  other stuff
?>
于 2014-11-07T07:10:10.007 回答
1

解决了。您需要将 POST 编码为 utf8 。

 if(method == "POST"){
            // request method is POST
            // defaultHttpClient

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8")); <------ utf8
于 2014-01-22T10:58:41.383 回答
0

您没有提供如何检索数据的代码片段。这是我的猜测,假设源字符集是 UTF-8,相应地替换为您的源编码。

InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));

str 是您的 json 字符串。

更新:似乎您使用 检索iso-8859-1,因此尝试将您的代码更新为UTF-8.

注意:JSON 文本的字符编码始终为 Unicode。UTF-8 是唯一在线上有意义的编码,但也允许使用 UTF-16 和 UTF-32 ( source )。

于 2012-11-09T07:36:40.430 回答
0
public static String test(){
String testResult= "";
try {
    HttpGet get = new HttpGet("http://xxxxx");//edit url removed.
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(get);
    String result = EntityUtils.toString(response.getEntity());
    JSONObject obj = new JSONObject(result);
    if(!obj.isNull("title")){
        testResult= obj.getString("title");
        Log.d("Test","Test1:"+ testResult);
    }
} catch (JSONException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return testResult;//

}

从此代码中获取帮助..它肯定会帮助你..享受

于 2012-11-09T07:32:49.287 回答