Volley框架之使用GET或POST将cookie传给服务器

Volley框架使用GET或POST将cookie传给服务器,需要覆盖一个方法:public Map<String, String> getHeaders() throws AuthFailureError

具体方法如下:

    private void VolleyGETCredit() {
        String url = "你的网址";
        RequestQueue queue = Volley.newRequestQueue(getActivity());
        StringRequest request= new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                       LoadHTML(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("获取失败");

                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headerMap = new HashMap<>();
                headerMap.put("Cookie", cookie);
                return headerMap;
            }
        };
        request.setTag("GET");
        queue.add(request);
    }
这个cookie从哪里来呢?请看我上一篇文章,我有提到,把得到的cookie使用 SharedPreferences

保存起来了。在前面,我是这么写的:

    private String cookie;
    public SharedPreferences preferences;
    public SharedPreferences.Editor editor;
    private WebView creditFragment_webView;
    private TextView creditFragment_textView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_credit, container, false);
        preferences = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
        cookie = preferences.getString("Cookie", null);
        creditFragment_webView = (WebView) view.findViewById(R.id.creditFragment_webView);
        creditFragment_textView = (TextView) view.findViewById(R.id.creditFragment_textView);
        VolleyGETCredit();
        return view;
    }

下一篇,使用Volley读取服务器的中文字符。


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页