0

PHP を使用して Android プロジェクトの基本的なアカウント作成を作成しようとしています。スクリプトに電子メール、パスワード、ニックネームを投稿すると、データベースにクエリを実行して重複を探し、見つからない場合は情報を挿入します。PHPスクリプトを実行するだけでは問題なく動作しますが、Androidから投稿しようとするとうまくいきません。コードは次のとおりです。

public String createUser(String email, String pass, String nick) {

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email", email));
    nameValuePairs.add(new BasicNameValuePair("password", pass));
    nameValuePairs.add(new BasicNameValuePair("nick", nick));

    InputStream is = null;
    String reply = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();

        // must store something locally to define this url
        // if the url ever changes, gotta make sure droids know without
        // requiring an update
        HttpPost httppost = new HttpPost(STR_PHP_USER);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);


        reply = reader.readLine();
        Log.e("createUser", "php response is "+reply);
        is.close();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString() + " getCreateUserResult_HTTPPost");
    }

    return reply;

}

スクリプトは、二重失敗の場合は 0、電子メールの失敗の場合は 1、ニックネームの失敗の場合は 2、成功の場合は 3 を返す必要があります。Android プロジェクトから、空の名前とニックネームを持つユーザーがデータベースに既に存在するため、私は常に 0 を取得しています。

また、print_r($_GET, true));結果は次のようになります。

Array
(
)

Androidプロジェクトを実行すると、

Array
(
    [email] => Random@someplace.org
    [password] => somepassword
    [nick] => RandoTheMagnificent
)

ブラウザから行う場合。

何か案は?

4

1 に答える 1

0

Android アプリでは、GET ではなく POST を使用してパラメーターを送信しています。

POST を使用するようにスクリプトを変更する代わりに、GET を使用する必要があります。

PS を追加して確認することもできますprint_r($_POST)

于 2012-07-02T15:31:41.447 に答える