我有一个全屏网络视图,其中:
1)我将 xml 文件发送到 php 脚本
2)php 脚本使用 javascript 构建页面并回显整个代码
3)webview 加载(应该加载)结果。
从脚本读取结果,我可以看到整个 html 代码,但 web 视图没有加载它。这是带有结果页面的 logcat:http: //pastebin.com/yjKK10SY
这就是我处理请求/响应客户端的方式:
@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;
}
}
}
活动开始时,屏幕保持空白,我在哪里做错了?