版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30519365/article/details/54613269
1,compile 'org.xutils:xutils:3.3.40'
2.
import org.xutils.x; public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this);//这点有点怪怪不知道为啥小写,而且需要手动导入包 x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能. } }
3.常用请求封装(不很完善)
package com.example.administrator.xutilsdemo; import android.util.Log; import android.view.View; import android.widget.ImageView; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.Map; /** * Created by Administrator on 2017/1/19. */ public class XutilsNetUtils { public interface GscCallBack{ public void onSuccess(String result); public void onError(Throwable ex, boolean isOnCallback); } public void doGet(Map<String,String>map, String URL, final GscCallBack callBack){ RequestParams entiy=new RequestParams(URL); if (map!=null){ for(Map.Entry<String,String> m:map.entrySet()){ entiy.addBodyParameter(m.getKey(),m.getValue()); } } x.http().get(entiy, new Callback.CacheCallback<String>() { @Override public void onSuccess(String result) { callBack.onSuccess(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { callBack.onError(ex,isOnCallback); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } }); } public void doPost(Map<String,String>map, String URL, final GscCallBack callBack){ RequestParams entiy=new RequestParams(URL); if (map!=null){ for(Map.Entry<String,String> m:map.entrySet()){ entiy.addBodyParameter(m.getKey(),m.getValue()); } } x.http().post(entiy, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { callBack.onSuccess(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { callBack.onError(ex,isOnCallback); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } public void loadImage(String Url, ImageView v){ x.image().bind(v,Url); } }
package com.example.administrator.myplayerdemo.activitys; import org.xutils.db.annotation.Column; import org.xutils.db.annotation.Table; /** * Created by Administrator on 2017/3/10 0010. */ @Table(name="db")//和数据库表明对应 public class Movies { //isId=true autoGen=true 表示是不是主键,是不是自动增长 @Column(name="id",isId=true,autoGen=true) private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.example.administrator.myplayerdemo.activitys; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.example.administrator.myplayerdemo.MyApp; import com.example.administrator.myplayerdemo.R; import org.xutils.DbManager; import org.xutils.ex.DbException; import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.Event; import org.xutils.view.annotation.ViewInject; import org.xutils.x; import java.io.File; import java.util.List; /** * Created by Administrator on 2017/3/10 0010. * 添加地址到数据库 */ @ContentView(value = R.layout.a) public class AddPage extends Activity { private DbManager db; @ViewInject(R.id.tv) private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.a); //初始化db 配置 设置数据库名字,版本号 x.view().inject(this); initdbconfig(); tv.setText("lalala"); } public void add(View view){ try { Movies m=new Movies();//创建bean m.setName("zhangsan");//Model赋值 db.save(m);//保存到db Toast.makeText(getApplicationContext(),"插入数据成功!",Toast.LENGTH_LONG).show(); } catch (DbException e) { e.printStackTrace(); } } @Event(value = R.id.tv,type = View.OnClickListener.class) private void click(View v){ Toast.makeText(getApplicationContext(),"点我干毛线",Toast.LENGTH_LONG).show(); } private void initdbconfig() { DbManager.DaoConfig daoConfig = new DbManager.DaoConfig() .setDbName("db.db") .setAllowTransaction(true) .setDbVersion(1) .setDbUpgradeListener(new DbManager.DbUpgradeListener() { @Override public void onUpgrade(DbManager db, int oldVersion, int newVersion) { // db.addColumn(...); // db.dropTable(...); // ... //更新数据库 } }); db = x.getDb(daoConfig);//创建db管理对象 } public void selectdb(View view ){ if (db!=null){ try { List<Movies> all = db.findAll(Movies.class);//查询所有 //db.findById(Movies.class,1); 根据id查询 // Movies first = db.findFirst(Movies.class);查询第一条数据 // Log.i("gsc",first.getName()+"******"+first.getId()); for (int i=0;i<all.size();i++){ String name = all.get(i).getName(); int id = all.get(i).getId(); Log.i("gsc",name+id); } } catch (DbException e) { e.printStackTrace(); } } else{ Toast.makeText(getApplicationContext(),"请先插入数据!",Toast.LENGTH_LONG).show(); } } }