volley使用
来源:互联网 发布:基于weka的数据挖掘 编辑:程序博客网 时间:2024/06/07 00:55
volley是谷歌推出的获取网络数据的方法,与okhttp相比更加灵活,快捷,图片处理比较好,唯一缺点就是不适合上传下载,整合了一个小例子几行代码搞定很便捷。
//首先在应用层定义成为全局可用的方便整体修改
public class MyApplication extends Application { public static RequestQueue queue; @Override public void onCreate() { super.onCreate();queue = Volley.newRequestQueue(getApplicationContext()); } public static RequestQueue getHttpQueue(){ return queue; }}//此处是javabean类存储json数据
public class JavaBean {
public String resultcode;
public String reason;
public Result result;
public class Result{ public String province; public String city; public String areacode; public String zip; public String company; public String card;}
}
这里写代码片
//volley的用法 在此处用的是Gson解析json
public class MainActivity extends Activity { private Editable num; private TextView tv; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText et = (EditText)findViewById(R.id.et); num = et.getText(); btn = (Button)findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { volleg_Get(); } }); } private void volleg_Get() { //这个是请求数据的接口 String url="http://apis.juhe.cn/mobile/get?phone="+num+"&key=3a6e26e2195864bdf40ec4d39e5e9a92"; //在此处不确定返回格式是json还是xml用的stringrequest 如果知道可以用JsonArrayRequest StringRequest request = new StringRequest(Method.GET, url, new Listener<String>() { //请求成功 @Override public void onResponse(String arg0) { ArrayList<String> list = new ArrayList<String>(); list.clear(); Gson gson = new Gson(); JavaBean bean = gson.fromJson(arg0, JavaBean.class); list.add(bean.result.province); list.add(bean.result.city); list.add(bean.result.areacode); list.add(bean.result.zip); list.add(bean.result.company); list.add(bean.result.card); for (int i = 0; i < list.size(); i++) { tv.setText(list.get(i)); } } },new Response.ErrorListener(){ //请求失败 @Override public void onErrorResponse(VolleyError arg0) { Toast.makeText(getApplication(), arg0.toString(), 0).show(); } }); //设置一个标签便于操纵队列 request.setTag("get"); MyApplication.getHttpQueue().add(request); } }//别忘了在关闭该页的时候清除掉队列避免内存的消耗@Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); MyApplication.getHttpQueue().cancelAll("get"); }
最后别忘了添加权限
0 0
- Volley 使用
- Volley使用
- volley使用
- Volley使用
- volley使用
- Volley使用
- Volley使用
- Volley使用
- 使用 Volley
- Android Volley,使用Volley加载网络图片
- Android Volley使用之一:Volley基础用法
- Volley框架(一):使用Volley请求数据
- Volley框架(二):使用Volley加载图片
- Volley框架(四):使用Volley上传文件
- Android Volley 使用Volley加载网络图片
- Volley的初步使用
- Android,Volley使用介绍
- Volley框架的使用
- actionBar之searchView
- mybatis的sql中使用concat查询结果是乱码处理
- Socket编程中如何发送结构体,拼接多次接收消息及获取通信双方地址
- JSTL标签库
- Android 编程下的代码混淆
- volley使用
- GestureDetector_OnGestureListener
- Git stash
- DDD领域驱动设计基本理论知识总结
- “不能设置下面的断点,断点为能绑定”的问题
- sprintf函数把格式化的字符串写入变量中
- scaletype之centerInside,center,centerCrop,fitCenter之间的区别
- Linux 定时任务
- maven 自动发布更新本地和远程仓库