Json + webview, basic doubt!

Asked

Viewed 177 times

1

Good morning!

I have a Webview, where I receive a JSON and I intend to build Webview according to the data received. In that part of the code html += "<h1>" + nmTitle + "</h1>"; i put the variable that receives the JSON content in that part nmTitle = object.optString("nmTitle");, that is above in the code, inside the Handler that receives the JSON.

However, instead of receiving the content assigned, my Webview displays the title as null, apparently, it runs Webview first and then receives JSON. How to change this so it assigns content and then sends 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);

    }
}

2 answers

1

You better get a return of json via javascript via 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> 
  • 1

    Thanks for the help, but I managed to resolve by simply putting the layout statements below Handler

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);
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.