【安卓学习日记——xUtils】
文章图片
文章图片
文章图片
https://github.com/qxbl/xUtils
下载完之后,解压,导入jar包
DbUtils的使用
权限设置
User
package com.example.xutils;
public class User {
private int id;
private String name;
private String email;
public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public User(String email, String name) {
this.email = email;
this.name = name;
}public String getEmail() {
return email;
}public void setEmail(String email) {
this.email = email;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public User() {
}@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", id=" + id +
", name='" + name + '\'' +
'}';
}
}
public class MainActivity extends AppCompatActivity {
DbUtils dbUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbUtils= DbUtils.create(this,"mydb");
}
添加
public void saveClick(View v){
User user=new User("ck@qq.com","ck");
try {
dbUtils.save(user);
Toast.makeText(this, "save success", Toast.LENGTH_SHORT).show();
} catch (DbException e) {
e.printStackTrace();
}
}
文章图片
查询
public void findAllClick(View v){
try {
List list=dbUtils.findAll(User.class);
for (User u:list)
{
System.out.println(u);
}
} catch (DbException e) {
e.printStackTrace();
}
}
文章图片
条件查询
public void selectByClick(View view)
{
try {
User user = dbUtils.findFirst(Selector.from(User.class).where("name","=","ck"));
Toast.makeText(this,user.toString(),Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
文章图片
更新
public void updateClick(View view)
{
User user=new User();
user.setId(1);
user.setEmail("chenke@qq.com");
user.setName("chenke");
try {
dbUtils.update(user,"email","name");
Toast.makeText(this,"更新成功",Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
文章图片
文章图片
删除
public void deleteClick(View view)
{
try {
dbUtils.delete(User.class, WhereBuilder.b("id","=","1"));
Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
} catch (DbException e) {
e.printStackTrace();
}
}
文章图片
文章图片
ViewUtils的使用
public class MainActivity extends AppCompatActivity {
DbUtils dbUtils;
@ViewInject(R.id.textView_data)
private TextView textView_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
//注入view和事件
dbUtils= DbUtils.create(this,"mydb");
}
@OnClick(R.id.button_show)
public void show(View v){
textView_data.setText("viewUtils的使用");
}
文章图片
文章图片
HttpUtils由于没去写服务器,所以这边不好测试,就没弄了
package com.example.xutils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
public class Main2Activity extends AppCompatActivity {
@ViewInject(R.id.editText_username)
EditText editText_username;
@ViewInject(R.id.editText_password)
EditText editText_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewUtils.inject(this);
//注入view和事件
}@OnClick(R.id.button_login)
public void login(View v){
String username=editText_username.getText().toString();
String password=editText_password.getText().toString();
HttpUtils httpUtils=new HttpUtils();
String url="";
//服务器地址
RequestParams params=new RequestParams();
params.addQueryStringParameter("username",username);
//服务器那边对应的参数名称
params.addBodyParameter("password",password);
//服务器那边对应的参数名称
httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack() {@Override
public void onSuccess(ResponseInfo responseInfo) {
String result=responseInfo.result;
if ("1".equals(result)){
Toast.makeText(Main2Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Main2Activity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}@Override
public void onFailure(HttpException e, String s) {
Toast.makeText(Main2Activity.this, s, Toast.LENGTH_SHORT).show();
}
});
}}
使用HttpUtils时出现了“无法访问HttpRequestBase 找不到org.apache.http.client.methods.HttpRequestBase的类文件”这个错误,原因是
android 6.0(api 23) SDK,Android的网络请求强制使用HttpUrlConnection,并且SDK中也已经移除了HttpClient。
解决办法
在build.gradle中添加
android {
useLibrary'org.apache.http.legacy'
}
就可以了