現在、Android SDKとPHPサーバーサイドスクリプトを使用して、自分のWebサイト用のアプリケーションを開発しています。
とリクエストを介してPOSTデータを送信し、結果を取得username
しよpassword
うとしています( )。残念ながら、は正しく機能していません。リクエストが送信され、結果が私のPHPスクリプトによって返送されますが、はまだ機能していません。HttpClient
HttpPost
echo $result;
HttpResponse
Intent
私のPHPコードがあります(私は私のDB情報を隠しました、あなたは理由を理解するでしょう!):
<?php
$con = mysql_connect('localhost','','');
mysql_select_db('', $con);
$username = $_POST['username'];
$password = $_POST['password'];
$users = mysql_query("SELECT * FROM `` WHERE `username`='".$username."' AND `password`='".$password."'");
if (mysql_num_rows($users) == 1)
{
$usr = mysql_fetch_array($users);
$usr['username'];
}
else
{
echo "error";
}
?>
そして、私のJavalogin()
メソッドがあります:
public void login()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.creationsmicroweb.com/app/login.php");
String username = (String) findViewById(R.id.usr1).toString();
String password = (String) findViewById(R.id.pwd1).toString();
Button login = (Button) findViewById(R.id.button1);
try
{
// Disabling other request
login.setEnabled(false);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("http", "Executing HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String status = inputStreamToString(response.getEntity().getContent()).toString();
Integer code = response.getStatusLine().getStatusCode();
if (!status.toString().equalsIgnoreCase("error"))
{
Intent myIntent = new Intent(this.getBaseContext(), HomeActivity.class);
myIntent.putExtra("username", status);
startActivityForResult(myIntent, 0);
Toast.makeText(this, "You've been connected!", Toast.LENGTH_LONG).show();
Log.i("login", "Logged in // " + response);
}
else
{
Toast.makeText(this, "Bad username/password", Toast.LENGTH_LONG);
Log.i("login", "Bad username/password // " + response);
login.setEnabled(true);
}
}
catch (ClientProtocolException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("http", e.getMessage());
login.setEnabled(true);
}
catch (IOException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("http", e.getMessage());
login.setEnabled(true);
}
}
private Object inputStreamToString(InputStream is)
{
// TODO Auto-generated method stub
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try
{
while ((line = rd.readLine()) != null)
{
total.append(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
// Return full string
return total;
}
PHPによって返された応答は、私のJavaコードでは正しく機能していません。明らかに、ユーザーが「ログイン」を押すと、資格情報がPHPに送信され、PHPは回答(ユーザー名またはエラー)を返します。その後、Javaコードは、答えが「エラー」に等しいかどうかをチェックしますが、これは機能していません。ログインは成功しましたが(すべてログに記録します)、Intentメソッドが実行されず、Toastメッセージが表示されません。
明らかに、私はリクエストが正しく機能しない理由を知りたいと思っています。私はJavaを始めているので、私は最高ではありません!