将Json文本解析为问号

时间:2013-05-14 14:11:56

标签: android json mysqli android-sqlite

我正在使用Json从sql数据库获取文本。

有许多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);
}
?>

1 个答案:

答案 0 :(得分:0)

尝试在您的数据库中使用cp1256_general_ci而不是UTF-8_general_ci ..还可以将它作为字段的排序规则。