根据volley网络请求框架封装好的get请求和post请求
2015-09-08 10:08
666 查看
下面这个类已经封装好了volley的get请求和post请求,直接
调用就可以
import java.util.HashMap; import java.util.Map; import com.android.volley.AuthFailureError; import com.android.volley.RequestQueue; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.StringRequest; public class HttpUtils { /**get请求例子*/ public static void getHomeData(RequestQueue queue, Listener<String> listener, ErrorListener error) { Map<String, String> map = new HashMap<String, String>(); map.put("PageSize", "10"); map.put("pageIndex", "1"); getHttpRequest(queue, “www.hhhhh.com”, map, listener, error); } /**post请求例子*/ public static void getGLHT(RequestQueue queue, Listener<String> listener, ErrorListener error, String pageSize) { Map<String, String> map = new HashMap<String, String>(); map.put("pagesize", pageSize); map.put("days", "1"); map.put("cityId", "226"); map.put("checkintime", "2014/12/29"); postHttpRequest(queue," www.hhhhh.com”, map, listener, error); } /** post请求 */ public static void postHttpRequest(RequestQueue queue, String url, final Map<String, String> map, Listener<String> listener, ErrorListener error) { StringRequest request = new StringRequest(url, listener, error) { @Override protected Map<String, String> getParams() throws AuthFailureError { // TODO Auto-generated method stub if (map != null) { return map; } return super.getParams(); } }; queue.add(request); } /** get请求 */ public static void getHttpRequest(RequestQueue queue, String url, Map<String, String> map, Listener<String> listener, ErrorListener error) { StringRequest request = new StringRequest(paramsCastUrl(url, map), listener, error); queue.add(request); } /** 把map参数 拼接成 get请求的 url格式 ,最后和 传过来的url一起拼接 */ public static String paramsCastUrl(String url, Map<String, String> map) { if (map != null) { String params = "?"; /** 遍历map,把 键值对应 */ for (Map.Entry<String, String> entry : map.entrySet()) { params += entry.getKey() + "=" + entry.getValue() + "&"; } /** 把一个字符串 从 0 一直截取到 字符串减一个长度处 */ params = params.substring(0, params.length() - 1); return url + params; } return url; } }
调用就可以
相关文章推荐
- Windows下挂载Ubuntu网络文件系统NFS
- https原理:证书传递、验证和数据加密、解密过程解析
- HTTP报文解析
- linux中网络接口名称限制
- linux 安装httpd(验证通过)
- apache日志提示[warn] (OS 64)指定的网络名不再可用。 : winnt_accept
- 卷积神经网络各参数的含义
- VMware下redhat与主机网络互通配置
- iOS开发之-- DNS解析(网络切换的问题解决)
- httpPost和httpGet的使用示例
- NFS 跨网问题解决
- 关于“50% CPU 占有率”题目的内容扩展
- 网络受限是个什么东东?
- 手把手教你写基于C++ Winsock的图片下载的网络爬虫
- 常见开源产品epoll网络事件模型分析(附200万QPS实现长连接echo server方案)
- HTTPS
- TCP 回顾
- JBoss 系列二:使用Apache httpd(mod_cluster)和JBoss构架高可用集群环境
- JSP报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- https://readthedocs.org/