WebView empty until I touch the component

空扰寡人 提交于 2019-12-25 03:27:44

问题


I'm doing a request with Volley Library. The response from the network is stored in a String. So, I load a webview with this data by the next way:

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

My problem is that the web is loaded, but It is only showed when I touch the webview. What I can do to update the view?

Source Code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register_oauth, container, false);

        //Queue of petitions
        volley_queue = Volley.newRequestQueue(container.getContext());

        webView = (WebView) view.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

        //MANDAR PETICION
        StringRequest postReq = new StringRequest(Request.Method.POST, pathto.get_register_service(), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VolleyReq", "onResponse\n " + response);
                webView.loadData(response, "text/html; charset=UTF-8", null);
                webView.reload();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("VolleyReq", "onResponse\n " + error);
            }

        });

        volley_queue.add(postReq);

        return view;
    }

Thank you.


回答1:


This code works to me:

getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadData(received_response, "text/html; charset=UTF-8", null);
                    }
                });


来源:https://stackoverflow.com/questions/25262567/webview-empty-until-i-touch-the-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!