1

問題に直面しています。有効な JSON 文字列を JSON オブジェクトにすることはできません。

サーバーからの応答をテストしましたが、これは有効な JSON です。

インターネットで調べたところ、DOMでのUTF-8の問題についてです。しかし、Notepad ++の文字セットをDOMなしでUTF-8に変更しても、同じエラーが引き続き発生します。

私のコード:

<?php
    require_once("Connection/conn.php");


    //parse JSON and get input
    $json_string = $_POST['json'];
    $json_associative_array = json_decode($json_string,true);

    $userId = $json_associative_array["userId"];
    $password = $json_associative_array["password"];
    $userType = $json_associative_array["userType"];    

    //get the resources
    $json_output_array = array();

    $sql = "SELECT * FROM account WHERE userId = '$userId' AND password = '$password' AND userType = '$userType'";  
    $result = mysql_query($sql);  

    //access success?
    if (!$result) {
        die('Invalid query: ' . mysql_error());
        $json_output_array["status"] = "query failed";
    }
    else{
        $json_output_array["status"] = "query success";
    }

    //find the particular user?
    if (mysql_num_rows($result) > 0){
        $json_output_array["valid"] = "yes";
    }
    else{
        $json_output_array["valid"] = "no";
    }

    //output JSON 
    echo json_encode($json_output_array);
?>

Android コード:

public boolean login() {
        // instantiates httpclient to make request
        DefaultHttpClient httpClient = new DefaultHttpClient();

        // url with the post data
        String url = SERVER_IP + "/gc/login.php";

        JSONObject holder = new JSONObject();
        try {
            holder.put("userId", "S1");
            holder.put("password", "s12345");
            holder.put("userType", "supervisor");
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Log.d("JSON", holder.toString());


        // HttpPost
        HttpPost httpPost = new HttpPost(url);


        //FormEntity
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("json", holder.toString()));

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // execution and response
        boolean valid = false; 
        try {
            HttpResponse response = httpClient.execute(httpPost);

            Log.d("post request", "finished execueted");
            String responseString = getHttpResponseContent(response);
            Log.d("post result", responseString);

            //parse JSON
            JSONObject jsonComeBack = new JSONObject(responseString);
            String validString = jsonComeBack.getString("valid");
            valid = (validString.equals("yes"))?true:false;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return valid;

    }

    private String getHttpResponseContent(HttpResponse response) {

        String responseString = "";

        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            String line = "";

            while ((line = rd.readLine()) != null) {
                responseString += line ;
            }
            rd.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return responseString;
    }

JSON はサーバーから取得されます:

{
    "status": "query success",
    "valid": "yes"
}

JSON のフォーマットを解除します。

{"status":"query success","valid":"yes"}

これをnotepad++にコピペすると?{"status":"query success","valid":"yes"} 「見えない文字があるらしい」となります。

4

2 に答える 2

2

MuhammedPasha が提供するソリューションで修正しました。これは、JSON 文字列をサブストリング化して、目に見えない文字を削除します。そして、問題を解決するために JSON 文字列を 1 から部分文字列化します。

これらの見えない文字を検出するには、ログ結果をメモ帳にコピーする方法があります (コピー! 入力しないでください!)。

于 2012-11-13T22:50:27.107 に答える
1

私は同じ問題を抱えていました。おそらく、Unicode 署名 (BOM) なしで保存する必要があります。

于 2013-02-23T18:57:03.457 に答える