网络请求
`public class CustomRequest extends Request
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}`
RequestQueue queue = Volley.newRequestQueue(this);
CustomRequest chapterecordRequest = new CustomRequest()
queue.add(chapterecordRequest)
推荐的库
android async http
https://github.com/loopj/android-async-http
屏幕适配
1.AndroidAutoLayout
https://github.com/androiddream/AndroidAutoLayout
2.android percent layout
https://github.com/JulienGenoud/android-percent-support-lib-sample
网络图片加载
NetworkImageView,Volley里带的,效果还不错
布局文件
<com.android.volley.toolbox.NetworkImageView
android:scaleType="fitCenter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView8"
android:layout_centerHorizontal="true"
>
</com.android.volley.toolbox.NetworkImageView>
自定义BitmapCache
`public class BitmapCache implements ImageCache {
private static final String TAG = "BitmapCache";
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
int maxSize = 4 * 1024 * 1024;
mCache = new LruCache<String, Bitmap>(maxSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
Log.i(TAG, "get cache " + url);
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
Log.i(TAG, "get cache: " + url);
if (bitmap != null) {
mCache.put(url, bitmap);
}
}
}使用示例
mQueue = Volley.newRequestQueue(getActivity());
final ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
imgviewnetwork.setDefaultImageResId(R.drawable.default);
imgviewnetwork.setErrorImageResId(R.drawable.error);
imgviewnetwork.setImageUrl(imgurl, imageLoader); `
推荐的其他库
Glide
https://github.com/bumptech/glide
Picasso
http://square.github.io/picasso/
fresco
https://github.com/facebook/fresco
Android-Universal-Image-Loader
https://github.com/nostra13/Android-Universal-Image-Loader
数据存储
就用了SharedPrefrences,Sqlite,很简单不贴代码了
推荐几个比较流行的orm框架
greenDAO
Android Sqlite orm 的 db 工具类
项目地址:https://github.com/greenrobot/greenDAO
文档介绍:http://greendao-orm.com/documentation/
官网网址:http://greendao-orm.com/
特点:(1) 性能佳
(2) 简单易用的 API
(3) 内存小好小
(4) 库大小小
ActiveAndroid
Android Sqlite orm 的 db 工具类
项目地址:https://github.com/pardom/ActiveAndroid
文档介绍:https://github.com/pardom/ActiveAndroid/wiki/_pages
Sprinkles
Android Sqlite orm 的 db 工具类,比较显著的特点就是配合 https://github.com/square/retrofit 能保存从服务器获取的数据
项目地址:https://github.com/emilsjolander/sprinkles
文档介绍:http://emilsjolander.github.io/blog/2013/12/18/android-with-sprinkles/
Realm
移动端的数据库,适用于 Phone、Tablet、Wearable,支持 ORM,线程安全、支持连表及数据库加密,比 SQLite 性能更好
项目地址:https://github.com/realm/realm-java
文档介绍:http://realm.io/docs/java/0.72.0/
ormlite-android
项目地址:https://github.com/j256/ormlite-android
文档介绍:http://ormlite.com/sqlite_java_android_orm.shtml
Schematic
根据 SQLite 生成 ContentProvider
项目地址:https://github.com/SimonVT/schematic
DBFlow
Android SQLite ORM 工具库。综合了 Active Android, Schematic, Ollie,Sprinkles 等库的优点;通过注解实现,性能好;能生成 ContentProvider。
项目地址:https://github.com/Raizlabs/DBFlow
文档介绍:https://github.com/Raizlabs/DBFlow#usage-docs