本文主要是介绍网络_volley_使用方法和代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Volley :Application 中:
HttpTools.init(context);
//Http 方式:
get(RequestInfo requestInfo, final HttpCallback httpResult);
post(RequestInfo requestInfo, final HttpCallback httpResult);
//下载方式:
DownloadRequest download(String url, String target, final boolean isResume, final HttpCallback httpResult)
DownloadRequest download(RequestInfo requestInfo, String target, final boolean isResume, final HttpCallback httpResult)
//文件上传:
Map<String,Object> params = new HashMap<String, Object>();
params.put("file0", new File("/sdcard/a.jpg"));
params.put("file1", new File("/sdcard/a.jpg"));
params.put("file2", new File("/sdcard/a.jpg"));
params.put("name", "张三");
mHttpTools.upload(url, params, httpResult);
MultiPartRequest<String> upload(final String url, final Map<String, Object> params, final HttpCallback httpResult)
MultiPartRequest<String> upload(RequestInfo requestInfo, final HttpCallback httpResult)
ImageRequest 和 DownloadRequest 不启用Gzip,其他请求均默认开启Gzip
Volley集成能了图片库.更加方便使用.
//图片库BitmapTools.init(context);
BitmapTools.stop();
//本地方法
Bitmap getBitmapFromRes(int resId);
Bitmap getBitmapFromAsset(String filePath);
Bitmap getBitmapFromContent(String imageUri);
Bitmap getBitmapFromFile(String path);
BitmapTools bitmapTools = new BitmapTools(mContext);
bitmapTools.display(view, uri);
bitmapTools.resume();
bitmapTools.pause();
配置类 BitmapDisplayConfig.java ;
bitmapTools.display(final View view, String uri, BitmapDisplayConfig displayConfig);
//数据库 DbTools模块
数据库模块集成了xUtils中DbUtils。
使用方法参考xUtils。
添加混淆配置:-keep class * extends java.lang.annotation.Annotation { *; }
RequestQueue mQueue = Volley.newRequestQueue(context);
//get 请求
StringRequest stringRequest = new StringRequest("http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("TAG", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } });
mQueue.add(stringRequest);
<uses-permission android:name="android.permission.INTERNET" />
StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener); StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("params1", "value1"); map.put("params2", "value2"); return map; }
};
//json 请求
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://m.weather.com.cn/data/101010100.html", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d("TAG", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } });
mQueue.add(jsonObjectRequest);
这篇关于网络_volley_使用方法和代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!