馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
package com.example.Family;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class enter extends Activity {
private Button btn1,btn2;
private EditText id,psw;
Thread Login;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.enter);
id=(EditText)findViewById(R.id.editText1);
psw=(EditText)findViewById(R.id.editText2);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
//Intent intent = new Intent();
//intent.setClass(enter.this, home.class);
//enter.this.startActivity(intent);
Login = new Login();
Login.start();
int a=0;
if("".equals(id.getText().toString()))
a=1;
else if("".equals(psw.getText().toString()))
a=1; //如果是空密碼
if(a==1)
{
Toast toast = Toast.makeText(getApplicationContext(),
"腦袋空空嗎~請輸入!", Toast.LENGTH_LONG);
toast.show();
}
}
}
);
btn2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(enter.this, new1.class);
enter.this.startActivity(intent);
}
});
}
class Login extends Thread{
@Override
public void run(){
//建立HttpClient用以跟伺服器溝通
HttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost("http://192.192.140.227/~D10216216/aaa.php");
//建立POST的變數
List< NameValuePair> vars = new ArrayList<NameValuePair>();
vars.add(new BasicNameValuePair("member_id",id.getText().toString()));
vars.add(new BasicNameValuePair("password",psw.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(vars,HTTP.UTF_8));
//建立ResponseHandler,以接收伺服器回傳的訊息
ResponseHandler<String> h=new BasicResponseHandler();
//將回傳的訊息轉為String
String response=new String(client.execute(post,h).getBytes(),HTTP.UTF_8);
String success = "Login Succeeded";
Looper.prepare();
//若回傳的訊息等於"Login Succeeded",跳轉到另一個頁面
if(response.equals(success)){
Intent i = new Intent(enter.this,home.class);
startActivity(i);
}
//否則只顯示回傳訊息
else{
Toast.makeText(enter.this, response, Toast.LENGTH_LONG).show();
}
Looper.loop();
}
catch(Exception ex){
//若伺服器無法與PHP檔連接時的動作
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
|
評分
-
查看全部評分

|