0

我正在使用 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);
}
?>
4

1 回答 1

0

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

于 2013-11-13T21:14:27.460 回答