Json + webview, basic doubt!

1

Good morning!

I have a WebView, where I get a JSON and want to mount the WebView according to the data received. In this part of the code html += "<h1>" + nmTitle + "</h1>"; I put the variable that receives the JSON content in that part nmTitle = object.optString("nmTitle"); , which is above the code, inside the handler that receives JSON.

However, instead of receiving the assigned content, my WebView displays the title as null , it seems, it runs WebView first and then receives JSON. How to change this for it to assign the content and then send it to HTML?

public class InfoFragment extends Activity {
    private AbstractNetworkHandler handler;
    String nmTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activities_list2);
        final WebView wv = new WebView(this);

        final LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        wv.setLayoutParams(lp);

        //this.handler = AbstractNetworkFactory.build(this, NetworkAction.WEB_VIEW);
        this.handler = AbstractNetworkFactory.build(super.getBaseContext(), NetworkAction.WEB_VIEW);
        Log.i("webview", "ate aki foi");

        handler.get(23L, new HttpJsonObjectListener() {

            @Override
            public void onRequestCompleted(final JSONObject object, Integer httpStatus, CharSequence msg) {
                // final JSONArray array = object.optJSONArray("searchResultsCollection");
                nmTitle = object.optString("nmTitle");

                final JSONArray array = object.optJSONArray("searchResultsCollection");

                String s = String.valueOf(nmTitle);

                // json.setText("My Awesome Text");
                Log.i("webview", s);
                Log.i("webview", "recebendo json");
            }

        }, new HttpFailListener() {
            @Override
            public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
                Log.i("webview", "falhou ao obter json");
            }
        });
        // (WebView) findViewById(R.id.webView1);
        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);

        // wv.loadUrl("http://www.thiengo.com.br/img/system/logo/thiengo-80-80.png");
        String html = "<html>";
        html += "<body>";
        html += "<h1>" + nmTitle + "</h1>";
        html += "<img src=\"http://www.exemplo.com.br/img/system/logo/-80-80.png\" style=\"float: left; display: block; margin-right: 10px;\" />";
        for(int i = 0; i < 3; i++) {
            html += "<h3 id=\"h3\" style=\"float: left;\">Texto auxiliar " + (i + 1) + "</h3>";
        }
        html += "<script type=\"text/javascript\">";
        html += "document.getElementById('h3').style.color = '#ff0000';";
        html += "</script></body></html>";

        wv.loadData(html, "text/html", "UTF-8");
        ll.addView(wv);

    }
}
    
asked by anonymous 05.11.2015 / 14:46

2 answers

1

You'd better get a json return via javascript via an external html.

EXAMPLE:

WebView mWebView = (WebView) findViewById(R.id.webView1);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(this, "webConnector");
    mWebView.addJavascriptInterface(this, "toaster");
    mWebView.loadUrl("file:///android_asset/index.html");
    }

    public String load() {
        Log.e("OlaJavascript","OlaJavascript");
        return "{\"key\":\"data\"}";
    }

    public void print(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

and HTML

<html>
<head>
<title>Test</title>
<script type="text/javascript">

function loader() {
    var jsonData = window.webConnector.load();
    toaster.print(jsonData);
}

</script>
</head>
<body onload="loader()">
lol > dota 2
</body>
</html> 
    
05.11.2015 / 14:55
0
public class InfoFragment extends Activity {
    private AbstractNetworkHandler handler;
    String nmTitle;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activities_list2);
        final WebView wv = new WebView(this);

        final LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        wv.setLayoutParams(lp);

        //this.handler = AbstractNetworkFactory.build(this, NetworkAction.WEB_VIEW);
        this.handler = AbstractNetworkFactory.build(super.getBaseContext(), NetworkAction.WEB_VIEW);
        Log.i("webview", "ate aki foi");

        handler.get(23L, new HttpJsonObjectListener() {

            @Override
            public void onRequestCompleted(final JSONObject object, Integer httpStatus, CharSequence msg) {
                // final JSONArray array = object.optJSONArray("searchResultsCollection");
                nmTitle = object.optString("nmTitle");

                final JSONArray array = object.optJSONArray("searchResultsCollection");

                String s = String.valueOf(nmTitle);

                // json.setText("My Awesome Text");
                Log.i("webview", s);
                Log.i("webview", "recebendo json");

                String html = "<html>";
                html += "<body>";
                html += "<h1>" + nmTitle + "</h1>";
                html += "<img src=\"http://www.exemplo.com.br/img/system/logo/-80-80.png\" style=\"float: left; display: block; margin-right: 10px;\" />";
                for(int i = 0; i < 3; i++) {
                    html += "<h3 id=\"h3\" style=\"float: left;\">Texto auxiliar " + (i + 1) + "</h3>";
                }
                html += "<script type=\"text/javascript\">";
                html += "document.getElementById('h3').style.color = '#ff0000';";
                html += "</script></body></html>";

                wv.loadData(html, "text/html", "UTF-8");
            }

        }, new HttpFailListener() {
            @Override
            public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
                Log.i("webview", "falhou ao obter json");
            }
        });
        // (WebView) findViewById(R.id.webView1);
        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);
        // wv.loadUrl("http://www.thiengo.com.br/img/system/logo/thiengo-80-80.png");
        ll.addView(wv);
    }

}
    
05.11.2015 / 14:57