Xutils框架说明:
xUtils 包含了orm, http(s), image, view注解, 但依然很轻量级(246K), 并且特性强大, 方便扩展:
稳定的基石: AbsTask和统一的回调接口Callback, 任何异常, 即使你的回调方法实现有异常都会进入onError, 任何情况下onFinished总会让你知道任务结束了.
基于高效稳定的orm工具, http模块得以更方便的实现cookie(支持domain, path, expiry等特性)和缓存(支持Cache-Control, Last-Modified, ETag等特性)的支持.
有了强大的http及其下载缓存的支持, image模块的实现相当的简洁, 并且支持回收被view持有, 但被Mem Cache移除的图片, 减少页面回退时的闪烁…
view注解模块仅仅400多行代码却灵活的支持了各种View注入和事件绑定, 包括拥有多了方法的listener的支持.
官方地址:https://github.com/wyouflf/xUtils3
Xutils框架作用:
构建快速互联网开发中小型项目基石,gradle部署,一行代码解决项目中的网络请求,图片加载,本地缓存以及注解查询控件等功能,基于XUtils可实现咨询项目,电商项目,社区项目的快速开发上线。
注解的理解:
注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据),常见的作用有以下几种:
生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等
跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量。也是
在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。
package com.example.day_01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;
@ContentView(R.layout.activity_view)
public class ViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
}
@Event(value = R.id.click,type = View.OnClickListener.class)
private void click(View view){
Toast.makeText(this, "普通点击", Toast.LENGTH_SHORT).show();
}
@Event(value = R.id.longclick,type = View.OnLongClickListener.class)
private boolean longclick(View view){
Toast.makeText(this, "长按点击", Toast.LENGTH_SHORT).show();
return false;
}
@Event(value = R.id.checkbox,type = CheckBox.OnCheckedChangeListener.class)
private void checkbox(CompoundButton buttonView, boolean isChecked){
if (isChecked){
Toast.makeText(this, "选择了", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "取消了", Toast.LENGTH_SHORT).show();
}
}
}
package com.example.day_01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;
@ContentView(R.layout.activity_http)
public class HttpActivity extends Activity {
private static final String GETURL = “http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1”;
private static final String POSTURL = “http://www.kuaidi100.com/query”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
}
@Event(value = {R.id.btn_get,R.id.btn_post},type = View.OnClickListener.class)
private void click(View view){
switch (view.getId()){
case R.id.btn_get:
Get();
break;
case R.id.btn_post:
Post();
break;
}
}
private void Post() {
RequestParams requestParams = new RequestParams(POSTURL);
requestParams.addBodyParameter("type","zhongtong");
requestParams.addBodyParameter("postid","543495422389");
x.http().post(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
private void Get() {
RequestParams requestParams = new RequestParams(GETURL);
x.http().get(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(final String result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(HttpActivity.this, ""+result, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
ex.getStackTrace();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}
package com.example.day_01;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;
@ContentView(R.layout.activity_draw)
public class DrawActivity extends Activity implements View.OnClickListener {
private Button satrt;
private Button stop;
private ImageView img;
AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
initView();
animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_01),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_02),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_03),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_04),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_05),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_06),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_07),50);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_08),50);
img.setImageDrawable(animationDrawable);
}
private void initView() {
satrt = (Button) findViewById(R.id.satrt);
stop = (Button) findViewById(R.id.stop);
img = (ImageView) findViewById(R.id.img);
satrt.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.satrt:
Start();
break;
case R.id.stop:
Stop();
break;
}
}
private void Stop() {
animationDrawable.stop();
}
private void Start() {
animationDrawable.start();
}
}
package com.example.day_01;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.io.File;
@ContentView(R.layout.activity_download)
public class DownLoadActivity extends Activity {
ProgressDialog progressDialog;
@ViewInject(R.id.down)
private Button down;
String url = “http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk”;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog();
RequestParams requestParams = new RequestParams();
requestParams.setUri(url);
x.http().get(requestParams, new Callback.ProgressCallback() {
@Override
public void onSuccess(File result) {
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
progressDialog.dismiss();
}
@Override
public void onStarted() {
progressDialog.show();
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
progressDialog.setProgress((int) (current*100/total));
}
});
}
});
}
private void dialog() {
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("下载Apk");
progressDialog.setMessage("下载中");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
}
}
package com.example.day_01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;
import static com.example.day_01.R.layout.activity_db;
public class DbActivity extends AppCompatActivity {
Button bt_write;
Button bt_read;
DbManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_db);
bt_write = findViewById(R.id.write);
bt_read = findViewById(R.id.read);
x.view().inject(this);
DbManager.DaoConfig daoConfig = new DbManager.DaoConfig();
daoConfig.setDbVersion(1);
daoConfig.setDbName(“UserBean”);
dbManager = x.getDb(daoConfig);
bt_write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
write();
}
});
bt_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
read();
}
});
}
private void read() {
UserBean u = null;
try {
u = dbManager.findById(UserBean.class, "1");
} catch (DbException e) {
e.printStackTrace();
}
Toast.makeText(this, ""+u.toString(), Toast.LENGTH_SHORT).show();
}
private void write() {
UserBean userBean = new UserBean();
userBean.setAge("19");
userBean.setId("1");
userBean.setName("王五");
try {
dbManager.save(userBean);
} catch (DbException e) {
e.printStackTrace();
}
}
}
package com.example.day_01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;
@ContentView(R.layout.activity_bt)
public class BitmapActivity extends Activity implements View.OnClickListener {
private ImageView img;
private Button bnt_bitmap;
String url = “http://b-ssl.duitang.com/uploads/item/201703/30/20170330225122_TLMun.jpeg”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
initView();
}
private void initView() {
img = (ImageView) findViewById(R.id.img);
bnt_bitmap = (Button) findViewById(R.id.bnt_bitmap);
bnt_bitmap.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bnt_bitmap:
gatBitmap();
break;
}
}
private void gatBitmap() {
x.image().bind(img,url);
}
}