• volley post非json格式数据并获取json数据


    在使用JsonObjectRequest时无法post非json格式的数据,因而采用StringRequest获取到相应的数据后再转为json格式的数据。

    //这里的上下文需要讨论
    private static RequestQueue mRequestQueue = Volley.newRequestQueue(Global.getContext());
    
    public interface IResponse{
        void subscribeData(Object data);
    }
    
    /**Post方式从网络获取数据 */
        public static void postDataFromNet(final IResponse iResponse, String url, final HashMap<String,String> map){
            StringRequest request = new StringRequest(
                Request.Method.POST,
                url,
                new Response.Listener<String>() {
    
                    @Override
                    public void onResponse(String response) {
                        try {
                            iResponse.subscribeData(new JSONObject(response));           //将数据返回
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
    
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("#StringRequest:Error#", volleyError.getMessage());
                    }
                }){
    
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> header = new HashMap<>();
                    header.put("token", "9FOYT495E6BCFLOKYE36F8C618533C3E");
                    return header;
                }
    
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return map;
                }
            };
            request.setTag(""+url);
            mRequestQueue.add(request);
        }
  • 相关阅读:
    2014-04-23 总结
    14-5-13
    PHP
    14-5-8
    ajax
    14-5-6
    14-5-5
    PHP初解
    14-4-30
    14-4-29
  • 原文地址:https://www.cnblogs.com/rwxwsblog/p/5448530.html
Copyright © 2020-2023  润新知