Fujitina
Fujitina

Reputation: 129

webView.loadData from html code doesn't work

i have a full screen web view, where:
1) i send an xml file to a php script
2) the php script build the page using javascript and echo back the whole code
3) the webview load ( should load ) the result.

Reading the result from the script, i can see the entire html code, but the web view doesn't load it. here is the logcat with the result page: http://pastebin.com/yjKK10SY

and this is how i handle the request / response client side:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    final String url = "http://www.mysite.meh/google_api/test.php";
    String selected = getIntent().getStringExtra("com.example.simplerun.filename");
    final File file = new File("/storage/sdcard0/SimpleRun/"+selected + ".xml");
    Log.i("FILE SIZE: ", "" + file.length());

    final WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.setWebViewClient(new MyWebViewClient());

    Thread thread = new Thread()
    {
        public void run()
        {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
      MultipartEntity entity = new MultipartEntity();

      entity.addPart("userfile", new FileBody(file));
      httppost.setEntity(entity);
      HttpResponse response = httpclient.execute(httppost);
      String res = EntityUtils.toString(response.getEntity());
        Log.i("RESPONSE: ", res);
        //String uri = Uri.encode(res);
        mWebView.loadData(res, "text/html; charset=UTF-8", null);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
        }
    };
    thread.start();

    try
    {
        thread.join();

    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }



    /*
    Thread thread = new Thread()
    {
        public void run()
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            client.getParams().setParameter("userfile", file);

            try
            {
                HttpResponse response = client.execute(post);
                String res = EntityUtils.toString(response.getEntity());
                Log.i("RESPONSE: ", res);
                mWebView.loadData(res, "text/html", HTTP.UTF_8);

            }
            catch(ClientProtocolException e)
            {
                e.printStackTrace();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    };
    thread.start();

    try
    {
        thread.join();
    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }

    */

    /*
    WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setSaveFormData(true);
    mWebView.setWebViewClient(new MyWebViewClient());
    */
}

private class MyWebViewClient extends WebViewClient {
    @Override
    // show the web page in webview but not in web browser
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}

}

when the activity start, the screen remain blank, where i'm doing wrong?

Upvotes: 0

Views: 11882

Answers (3)

Jannie Theunissen
Jannie Theunissen

Reputation: 30114

You access the web in a background thread, so you need to make sure you output to the UI thread:

runOnUiThread(new Runnable() {
    public void run() {
        mWebView.loadData(res, "text/html; charset=UTF-8", null);
    }
});

Upvotes: 0

epiphany27
epiphany27

Reputation: 517

Try this

webView.loadData(res, "text/html", "UTF-8") instead.

Also, i'll rather suggest you to use AsyncTask. Create it as inner class. Do all your http operations in your doInBackground() method of AsyncTask. And then call webView.loadData(...) in onPostExecute() method of AsyncTask.

Ofcourse u'll have to declare your Webview instance as an instance variable in your outer-class (instead of defining it inside onCreate) so as to make it accessible in the inner AsyncTask class.

Upvotes: 2

Mohsen Afshin
Mohsen Afshin

Reputation: 13436

I would recommned loadDataWithBaseURL

webView.loadDataWithBaseURL(null, result, "text/html", "utf-8", "about:blank");

Upvotes: 2

Related Questions