2013-05-14 52 views
0

我正在使用Json從sql數據庫中獲取文本。解析Json文本爲問號

有許多Unicode格式,如UTF-8用於保存數據庫中的數據。

我用UTF-8保存文本。文字不是英文。它是波斯語或阿拉伯語。但Json顯示文本爲????。我認爲問題出自Json Parser。請幫忙。

public class JSONParser { 

     static InputStream is = null; 
     static JSONObject jObj = null; 
     static String json = ""; 

     // constructor 
     public JSONParser() { 

     } 

     // 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, "UTF-8")); 

        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, "UTF-8"), 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; 

     } 
    } 

我覺得問題不是來自android。因爲當我從瀏覽器直接訪問php文件時,它顯示阿拉伯文爲????。我敢肯定,我的數據庫是UTF-8_general_ci的PHP是象下面這樣:

<?php 

// array for JSON response 
$response = array(); 

// include db connect class 
define('__ROOT__', dirname(dirname(__FILE__))); 
require_once(__ROOT__.'/Appstore/db_connect.php'); 

// connecting to db 
$db = new DB_CONNECT(); 

// get all products from products table 
$total = mysql_query("SELECT *FROM Apps") or die(mysql_error()); 
$num_rows = mysql_num_rows($total); 
$result = mysql_query("SELECT *FROM Apps limit 0,10") or die(mysql_error()); 


// check for empty result 
if (mysql_num_rows($result) > 0) { 
// looping through all results 
// products node 
$response["products"] = array(); 

while ($row = mysql_fetch_array($result)) { 
    // temp user array 
    $product = array(); 
    $product["pid"] = $row["pid"]; 
    $product["name"] = $row["name"]; 
    $product["rate"] = $row["rate"]; 
    $product["short_description"] = $row["short_description"]; 
    $product["size"] = $row["size"]; 
    $product["thumb_url"] = $row["thumb_url"]; 

    // push single product into final response array 
    array_push($response["products"], $product); 
} 
// success 
$response["success"] = 1; 

// echoing JSON response 
echo json_encode($response); 
} else { 
// no products found 
$response["success"] = 0; 
$response["message"] = "No products found"; 

// echo no users JSON 
echo json_encode($response); 
} 
?> 
+0

你說你正在使用utf-8在數據庫中存儲文本,但是然後在你的bufferedreader中使用iso8859?這將完全破壞UTF文本。 – 2013-05-14 14:17:18

+0

我檢查了'BufferedReader reader = new BufferedReader(new InputStreamReader(is,「utf-8」),8);'。但它不起作用。 – 2013-05-14 14:24:02

+0

我在數據庫中使用了'UTF-8_general_ci'和'UTF-8_unicode_ci'。但它不起作用。 – 2013-05-14 14:24:59

回答

0

嘗試在數據庫中使用cp1256_general_ci代替UTF-8_general_ci ..而且我們作爲排序規則的領域了。