1

MySql に接続し、データを JSON にエンコードする PHP コードがあります。後でそれをフィルタリングして、特定の JSON オブジェクトを取得します。1 つの NameValuePair オブジェクトを使用している間は問題なく動作しましたが、今はユーザー名やパスワードなどの変数を使用したいと考えています。今、logcat Error parsing data .org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be convert to JSONArray. でこのアラートを受け取っています。

正しく動作するコードをどのように変更すればよいですか?

$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));

リクエストを送信するコード:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("usern",""+usr));
        nameValuePairs.add(new BasicNameValuePair("passw",""+psw));
        InputStream is = null; 
        String result = "";
        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/Bdarbas/getUserInfo.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

編集済み

$username = mysql_real_escape_string($_REQUEST['usern']);
$password = mysql_real_escape_string($_REQUEST['passw']);
$q=mysql_query("SELECT username, firstname, lastname, email, phone1, skype, city, description
FROM mdl_user WHERE username LIKE '$username' AND password LIKE '$password'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));
4

2 に答える 2

1

これがサーバー側に配置したコード全体である場合は、最初に値を取り込んで、このような方法$username$password使用する必要があると思います$_POST[]

$username=$_POST["usern"];
$password=$_POST["passw"];

と同じですpassword

現在のところ、変数には値がないため、SELECTステートメントは値を返し、値エラーが発生する形式でnullクライアントに送信されます。JSONnull

于 2012-04-10T20:16:58.487 に答える
1

あなたのコメントに基づいて、「ユーザー名だけを試しているときに LIKE を使用しましたが、うまくいきました。」

パスワードをどのような形式で保存していますか? プレーンテキスト(そうではないことを願っています)またはハッシュ化されていますか?ハッシュ化されている場合、どこでハッシュ化を実行しますか? php では、それがコード全体である場合はそうではありません。Java コードでそれを行いますか? そうでない場合は、それがあなたの答えです。

LIKEについては、もちろんうまくいきました。LIKE は = ほど厳密ではなく、かなりのオーバーヘッドが発生します。非常に単純な検索ステートメント (VERY SIMPLE) でのみ使用する必要があり、承認では使用しないでください。

于 2012-04-10T20:44:26.770 に答える