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
原创粉丝点击