网络_volley_使用方法和代码

2024-06-01 17:58

本文主要是介绍网络_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" />  



//post 请求
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_使用方法和代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021715

相关文章

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告: