首页 > 解决方案 > 如何在 Volley 中处理来自 API 的 HTML 响应

问题描述

我在我的 api 请求响应中获取原始 html 整页数据。我想在 webview 或任何 pdf 查看器中显示它们。已经尝试在 webview 中显示它们,但由于令牌问题对我不起作用。任何更好的方式或任何建议来处理这个问题?

回复

标签: androidhtmlapiandroid-volley

解决方案


url = //your url will be here

        StringRequest request = new StringRequest(Request.Method.GET, url, (String response) -> {

            Log.e("loadData: ",response);

            if(response != null){
                Response = response;

                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.dismiss();
                        System.out.println("Response" + response);

                        //webView.loadData(response,"text/html","utf-8");
                        //webView.loadData(response, "text/html; charset=UTF-8", null);

                        ****//here i used the response to load in a webview!****

                        webView.loadDataWithBaseURL(null, response, "text/html", "UTF-8", null);
                    }
                },2000);
            }

        }, error -> {
            error.printStackTrace();
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
                map.put("Authorization", token);
                return map;
            }
        };

        RequestQueue queue = Volley.newRequestQueue(RiderPringtinActivity.this);
        queue.add(request);

推荐阅读