当前位置:主页 > 移动开发 > Android代码 >

Android 开发中Volley详解及实例

时间:2023-02-19 10:53:56 | 栏目:Android代码 | 点击:

Android 开发中Volley详解及实例

最近在做项目的时候,各种get和post。简直要疯了,我这种啥都不了解的,不知道咋办了,然后百度看了下,可以用volley进行网络请求与获取,下面就介绍下volley的用法。

volley有三种方式:JsonObjectRequest,JsonArrayRequest,StringRequest。其实都是差不多了,举一反三就ok了,这里我就讲下JsonObjectRequest。

方法如下:

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST,null,Listener,ErrListener);

事例如下:

RequestQueue myQueue = Volley.newRequestQueue(getContext());
    Map<String, String> params = new HashMap<String, String>();
    params.put("dataStr", "2016/3/18");
    params.put("selectRow", "0");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params),new Response.Listener<JSONObject>() {

      public void onResponse(JSONObject jsonObject) {
        try {
          time.setText(jsonObject.optJSONObject("data").getString("CreateTime"));
          title.setText(jsonObject.optJSONObject("data").getString("Title"));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

    }, new Response.ErrorListener() {
      public void onErrorResponse(VolleyError volleyError) {
        Log.e("error", volleyError + "");
      }
    }) ;
    jsonObjectRequest.setTag("tag");
    myQueue.add(jsonObjectRequest);

这样就解决了。。我可以说因为这个,我从周五的下午忙到现在嘛….好了,你们可以试下能不能解决你现在的问题。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

相关文章