0

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?

Fujitina
  • 129
  • 1
  • 2
  • 16

3 Answers3

2

I would recommned loadDataWithBaseURL

webView.loadDataWithBaseURL(null, result, "text/html", "utf-8", "about:blank");
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • Thank you for reply me, but i have the same identical result, i can read the whole html code in logcat but nothing happen on the screen :( – Fujitina Oct 04 '13 at 14:47
  • It's probably because of your html data. WebView has to render the html code into elements, so ensure the html data can be rendered in a browser, elements likes html, body, title, .... – Mohsen Afshin Oct 04 '13 at 15:05
  • before of that i try to display the same stuff, with a difference: the xml file were not sended, but already on the server, the only think the activity did, is contact an url and show the result, in that case the map was correctly displayed, so i don't think it's a rendering problem ( and if i try with a browser all works fine too) – Fujitina Oct 04 '13 at 15:14
  • mmh i made an html document from the result in the log, and you're right the screen is blank, have to understand what's wrong – Fujitina Oct 04 '13 at 15:28
2

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.

epiphany27
  • 517
  • 1
  • 10
  • 23
0

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);
    }
});
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127