Android与PHP之间的网络通信Http篇(用户登录验证post传值)

//登录的login.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LoginActivity" 
    android:background="#29aac0">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="128dp"
            android:gravity="center"
            android:text="民大招生系统"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fff"
            android:textSize="25dp" />

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_marginTop="30dip">

            <EditText
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="请输用户名"
                android:textColor="#000" >

                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="请输登陆密码"
                android:inputType="textPassword"
                android:textColor="#000" >
            </EditText>
        </TableLayout>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dip" >

            <Button
                android:id="@+id/btlogin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="50dip"
                android:background="#39b55f"
                android:text="登陆" />

            <Button
                android:id="@+id/btreset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="50dip"
                android:background="#30ff0f"
                android:text="重置" />

            <Button
                android:id="@+id/btregister"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:height="50dip"
                android:background="#0000ff"
                android:text="注册" />
        </TableRow>
    </TableLayout>

</TableLayout>


//Activity Java代码

public class LoginActivity extends Activity {
	
	EditText user,pass;
	Button login,reset,register;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		findmyid();
		//为每个button添加点击事件
		login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				login();	
			}
		});
		
		reset.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				reset();
			}
		});
		
		register.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				register();
			}
		});
	}

	//为每个控件匹配ID
	private void findmyid() {
		// TODO Auto-generated method stub
		user=(EditText) findViewById(R.id.username);
		pass=(EditText) findViewById(R.id.password);
		login=(Button) findViewById(R.id.btlogin);
		reset=(Button) findViewById(R.id.btreset);
		register=(Button) findViewById(R.id.btregister);
		
	}
	
	
	/*
	*每个button的事件定义
	*/
	//登陆事件
	protected void login(){
	try{
		// 定义源地址
    		final String PostUrl = "http://10.0.2.2/zhaosheng/index.php/AndroidLogin/login"; 
    		// 创建一个Http请求
    		HttpPost httprequest = new HttpPost(PostUrl); 
    		
    		List postparams = new ArrayList();
    		postparams.add(new BasicNameValuePair("username",user.getText().toString()));
    		// 添加必须的参数
    		postparams.add(new BasicNameValuePair("password",pass.getText().toString())); 
    		httprequest.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8));
    		HttpResponse httpResponse = new DefaultHttpClient().execute(httprequest);
    		// 发送请求并获取反馈
    		// 解析返回的内容
    		if (httpResponse.getStatusLine().getStatusCode() != 404)
    		{
    			String result = EntityUtils.toString(httpResponse.getEntity());
    			Toast.makeText(getApplicationContext(), result,Toast.LENGTH_SHORT).show();
    		}
    	}catch (Exception e) {
    		
    	}
		
	}
	
	//重置事件
	protected void reset(){
		user.setText("");
		pass.setText("");	
	}	
	//注册事件
	protected void register(){
		
	}
}

//php服务端(写个控制器)

<?php
//与AndroidApp交互的控制器
class AndroidLoginAction extends Action {
	public function index(){

	}

	//用户登录方法
	public function login(){
		$user=M('user');
		$username=I("username");
		$password=I("password");
		$condition['username']=$username;
		$condition['password']=$password;
		if($user->where($condition)->select()){
			echo "SUCCESS";
		}
		else{
			echo "ERROR";
		}


	}
	//用户注册方法
	public function register(){

	}
}
?>

//在Androidmanifest.xml添加允许网络通信的语句

<uses-permission android:name="android.permission.INTERNET" />
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值