このアクティビティはログインに使用されます。ユーザー名とパスワードを "GET" によって PHP 検証ファイルに投稿すると、応答 (成功または失敗) が得られます。ただし、「エラーが発生しました」(成功でも失敗でもない)しか表示できません。コードは次のとおりです。
public class Login extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
findViewsById();
setListeners();
}
private Button btn_login;
private EditText edt_username;
private EditText edt_password;
private void findViewsById() {
edt_username = (EditText) findViewById(R.id.edt_username);
edt_password = (EditText) findViewById(R.id.edt_password);
btn_login = (Button) findViewById(R.id.btn_login);
}
private void setListeners(){
btn_login.setOnClickListener(login);
}
private Button.OnClickListener login = new Button. OnClickListener() {
public void onClick(View v) {
new startLogin().execute();
}
};
private class startLogin extends AsyncTask <Void, Void, Void> {
String result;
String url = "http://www.abc.com/verify.php";
String fail = "FAILED";
String error = "error";
final ProgressDialog progDlg = new ProgressDialog(Login.this);
String username = edt_username.getText().toString();
String password = edt_password.getText().toString();
protected void onPreExecute() {
super.onPreExecute();
progDlg.setTitle("Please wait");
progDlg.setMessage("Loading...");
progDlg.setIcon(android.R.drawable.ic_dialog_info);
progDlg.setCancelable(false);
progDlg.show();
//add username and password to the site
url += "?username=" + username;
url += "&password=" + password;
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
HttpResponse httpResponse = null;
try {
HttpGet httpGet = new HttpGet (url) ;
httpResponse = new DefaultHttpClient().execute (httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
result = EntityUtils.toString(httpResponse.getEntity());
}
else{
//connection error
result = error;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void unused){
if (result == fail) {
progDlg.cancel();
Toast.makeText(Login.this, R.string.fail, Toast.LENGTH_SHORT).show(); //R.string.fail = "username or password is wrong"
} else if (result == error) {
progDlg.cancel();
Toast.makeText(Login.this, R.string.error, Toast.LENGTH_SHORT).show(); //R.string.error = "there is a connection error"
}
else if (result == "SUCCESS"){
progDlg.cancel();
Intent it = new Intent ();
it.setClass (Login.this, Main.class);
startActivity (it);
} else{
progDlg.cancel();
Toast.makeText(Login.this, "error occure", Toast.LENGTH_SHORT).show();
}
}
}}
検証ファイルは次のとおりです。
<?php
$username = $_GET["username"];
$password = $_GET["password"];
$ourFileName = "users/" . $username . ".txt";
if (file_exists($ourFileName))
{
$fh = fopen($ourFileName, 'r');
$theData = fread($fh, 1024);
fclose($fh);
if ($password == $theData)
{
echo SUCCESS;
} else
{
echo FAILED;
}
}else {
echo FAILED;}
?>
LogCat
07-05 13:10:57.065: W/ActivityThread(274): Application com.android.safe is waiting for the debugger on port 8100...
07-05 13:10:57.085: I/System.out(274): Sending WAIT chunk
07-05 13:10:57.155: I/dalvikvm(274): Debugger is active
07-05 13:10:57.285: I/System.out(274): Debugger has connected
07-05 13:10:57.285: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.486: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.685: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.885: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.095: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.295: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.495: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.695: I/System.out(274): debugger has settled (1326)
07-05 13:10:58.985: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 805 objects / 58072 bytes in 43ms
07-05 13:10:59.255: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 222 objects / 10616 bytes in 42ms
07-05 13:11:45.615: W/KeyCharacterMap(274): No keyboard for id 0
07-05 13:11:45.615: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-05 13:12:05.345: D/dalvikvm(274): GC_FOR_MALLOC freed 6458 objects / 265088 bytes in 138ms
07-05 13:12:08.036: W/System.err(274): java.lang.IllegalArgumentException: Illegal character in query at index 52: http://panel.o-safe.net/vb_verify.php?username=guest &password=guest
07-05 13:12:08.046: W/System.err(274): at java.net.URI.create(URI.java:970)
07-05 13:12:08.046: W/System.err(274): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
07-05 13:12:08.046: W/System.err(274): at com.android.safe.Login$startLogin.doInBackground(Login.java:100)
07-05 13:12:08.056: W/System.err(274): at com.android.safe.Login$startLogin.doInBackground(Login.java:1)
07-05 13:12:08.056: W/System.err(274): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-05 13:12:08.056: W/System.err(274): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-05 13:12:08.066: W/System.err(274): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-05 13:12:08.066: W/System.err(274): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-05 13:12:08.066: W/System.err(274): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-05 13:12:08.066: W/System.err(274): at java.lang.Thread.run(Thread.java:1096)
07-05 13:12:08.076: D/AndroidRuntime(274): Shutting down VM
07-05 13:12:08.076: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 13:12:08.116: E/AndroidRuntime(274): FATAL EXCEPTION: main
07-05 13:12:08.116: E/AndroidRuntime(274): java.lang.NullPointerException
07-05 13:12:08.116: E/AndroidRuntime(274): at com.android.safe.Login$startLogin.onPostExecute(Login.java:127)
07-05 13:12:08.116: E/AndroidRuntime(274): at com.android.safe.Login$startLogin.onPostExecute(Login.java:1)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
07-05 13:12:08.116: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 13:12:08.116: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 13:12:08.116: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
07-05 13:12:08.116: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 13:12:08.116: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 13:12:08.116: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)