项目中加入xutils
compile 'org.xutils:xutils:3.3.42'
使用前加入权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
xml
<EditText
android:id="@+id/et_main_uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="admin" />
<EditText
android:id="@+id/et_main_upass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456" />
<Button
android:id="@+id/btn_main_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
一个简单登录的dom,这里就没有连接数据库,使用前初始化xutils
@Override
public void onCreate() {
super.onCreate();
//初始化XUtils
x.Ext.init(this);
}
在mainActivity中写调用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x.view().inject(this);
}
@Event(value = R.id.btn_main_login,type = View.OnLongClickListener.class)
private boolean login(View view){
//获取文本框的值
String name=et_main_uname.getText().toString();
String pass=et_main_upass.getText().toString();
//发送请求
RequestParams params=new RequestParams("http://192.168.43.196:8090/loginResult.xhtml");
params.addParameter("uname",name);
params.addParameter("upass",pass);
x.http().post(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
return true;
}
}