This activity is used for login.
It post the username and password to a PHP verify file by “GET” which will a response(either SUCCESS or FAILED)would be got.
But it can only show “error occure” (neither SUCCESS nor FAILED)
here are the code:
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();
}
}
}}
Here are the verify file:
<?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)
please make sure that the PHP script works fine by checking the same on a browser
i noticed the following errors in your code
==rather thanequalsmethod in ‘String’ class. so change all==resultcomparisons toequalsinonPostExecutemethod.updated code
onPostExecutemethod. what i mean is, it is not necessary that you always make changes to global variables. here variableresult. for this you need to change theAsyncTasksignature while extending it.so change
to
the third parameter is the
Resulttype. Now you have to change the following methods to adapt the above changeto
and
to
this will help you to return status strings directly from
doInBackGroundmethod toonPostExecute