xUtils3??
- 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???.
????
- ??????(??2G)??
- ????http??????(11???)
- ???????ORM, ?greenDao?????
- ????????????????...
- ??????gif(????????, ??gif????????), webp; ????, ??, ?????, ??????...
- ?3.5.0??????libwebpbackport.so, ???Android4.2??????webp????3.4.0??.
??Gradle???????????:
compile 'org.xutils:xutils:3.5.0'
????eclipse?? ??????aar??, ???zip??, ??jar??.
??????????sample???
????:
- ?????????: https://github.com/wyouflf/xUtils3/issues/149
- Cookie???: https://github.com/wyouflf/xUtils3/issues/125
- ??query??? http?????? header, url, body(???)??; query???url???(?)?????.
- ??body??? body????PUT, POST, PATCH, DELETE(???RFC2616?????????????, ??????)????.
- ???Http?????????: https://github.com/wyouflf/xUtils3/issues/191
?????
?????
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
???
// ?application?onCreate???? @Override public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
// ????debug??, ??debug?????.
...
}
??@Event????(@ContentView, @ViewInject???????sample??)
/** * 1. ????????, * 2. ?????????type???Listener????. * 3. ????value????: value={
id1, id2, id3
}
* 4. ???????{
@link org.xutils.event.annotation.Event
}
????. **/ @Event(value = R.id.btn_test_baidu1,
type = View.OnClickListener.class/*????, ???View.OnClickListener.class*/) private void onTestBaidu1Click(View view) {
...
}
????(??????sample??)
/** * ???????????: * ???? {
@link org.xutils.http.annotation.HttpRequest
}
* ?????????? {
@link org.xutils.http.app.ParamsBuilder
}
* * ?????????callback????, ??: * ???? {
@link org.xutils.http.annotation.HttpResponse
}
* ?????????? {
@link org.xutils.http.app.ResponseParser
}
* * ??: ?? org.xutils.sample.http ????? */ BaiduParams params = new BaiduParams();
params.wd = "xUtils"; // ????????multipart??, ?????????. // params.setMultipart(true);
// ?????? 1 // params.uploadFile = new File("/sdcard/test.txt");
// ?????? 2 // params.addBodyParameter("uploadFile", new File("/sdcard/test.txt"));
Callback.Cancelable cancelable
= x.http().get(params,
/**
* 1. callback???:
* callback?????????????{
@link org.xutils.http.loader.LoaderFactory
}
,
* ??: ?????File????????, ??params.setSaveFilePath(path)??????????.
* ????????(???????????????????).
* ???????????LoaderFactory???,
* ?????{
@link org.xutils.http.annotation.HttpResponse
}
* ???HttpResponse???????????, ?????ResponseParser???????.
* ??????json??, ????????json????????????ResponseParser.
* ?????{
@link org.xutils.sample.http.BaiduResponse
}
, ?????BaiduResponse??
* callback???.
*
* 2. callback???:
* ??????????????Callback, ?{
@link org.xutils.common.Callback
}
.
* ??:
* a. ????CacheCallback????????????????(?GET????).
* b. ????PrepareCallback?prepare????callback???????????????,
* ??????onCache?onSuccess.
* c. ????ProgressCallback???????.
* ...(???{
@link org.xutils.image.ImageLoader
}
* ? ?????? {
@link org.xutils.sample.download.DownloadCallback
}
)
*
* 3. ???????????: ?? {
@link org.xutils.http.app.RequestTracker
}
*
* 4. ??Header??: ?? {
@link org.xutils.http.app.RequestInterceptListener
}
*
* 5. ??(???, ??, ???, ??, ???): ?? {
@link org.xutils.http.RequestParams
}
*
**/
new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
if (ex instanceof HttpException) {
// ????
HttpException httpEx = (HttpException) ex;
int responseCode = httpEx.getCode();
String responseMsg = httpEx.getMessage();
String errorResult = httpEx.getResult();
// ...
}
else {
// ????
// ...
}
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
}
}
);
// cancelable.cancel();
// ????
?????????????:
@Event(value = R.id.btn_test_baidu2) private void onTestBaidu2Click(View view) {
RequestParams params = new RequestParams("https://www.baidu.com/s");
params.setSslSocketFactory(...);
// ??ssl
params.addQueryStringParameter("wd", "xUtils");
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
}
}
);
}
?????????:
BaiduParams params = new BaiduParams();
params.wd = "xUtils"; // ????????, ??:??.(???????????max-age?Expires) params.setCacheMaxAge(1000 * 60);
Callback.Cancelable cancelable
// ??CacheCallback, xUtils?????????.
= x.http().get(params, new Callback.CacheCallback<String>() {
private boolean hasError = false; private String result = null;
@Override public boolean onCache(String result) {
// ??????, ?????????????.
// ?????????????, ??params.setCacheMaxAge(maxAge)??.
//
// * ???????????? header ? max-age ? expires ?????????? onCache ??.
//
????????? max-age ? expires, ?????????, ???????????false?
//
??, ??xUtils??????, ????.
//
// * ????????? true, ???????;
//
?? false ??????, ?????????ETag, Last-Modified???,
//
???????304, ?????????, ???????.
//
this.result = result;
return false; // true: ??????, ????????; false???????.
}
@Override public void onSuccess(String result) {
// ??: ??????304 ? onCache ???????, ??result?null.
if (result != null) {
this.result = result;
}
}
@Override public void onError(Throwable ex, boolean isOnCallback) {
hasError = true;
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
if (ex instanceof HttpException) {
// ????
HttpException httpEx = (HttpException) ex;
int responseCode = httpEx.getCode();
String responseMsg = httpEx.getMessage();
String errorResult = httpEx.getResult();
// ...
}
else {
// ????
// ...
}
}
@Override public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override public void onFinished() {
if (!hasError && result != null) {
// ??????
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}
}
}
);
?????(??????sample??)
Parent test = db.selector(Parent.class).where("id", "in", new int[]{
1, 3, 6
}
).findFirst();
long count = db.selector(Parent.class).where("name", "LIKE", "w%").and("age", ">", 32).count();
List<Parent> testList = db.selector(Parent.class).where("id", "between", new String[]{
"1", "5"
}
).findAll();
????(??????sample??)
x.image().bind(imageView, url, imageOptions);
// assets file x.image().bind(imageView, "assets://test.gif", imageOptions);
// local file x.image().bind(imageView, new File("/sdcard/test.gif").toURI().toString(), imageOptions);
x.image().bind(imageView, "/sdcard/test.gif", imageOptions);
x.image().bind(imageView, "file:///sdcard/test.gif", imageOptions);
x.image().bind(imageView, "file:/sdcard/test.gif", imageOptions);
x.image().bind(imageView, url, imageOptions, new Callback.CommonCallback<Drawable>() {
...
}
);
x.image().loadDrawable(url, imageOptions, new Callback.CommonCallback<Drawable>() {
...
}
);
// ???????? x.image().loadFile(url, imageOptions, new Callback.CommonCallback<File>() {
...
}
);
????
- Email? [email protected], [email protected]
- ??????????????????????, ??????QQ??330445659(??), 275967695, 257323060, 384426013, 176778777, 169852490, 261053948, 330108003, ?????idea?? _