1.用okhttp这个框架用https请求获取到html样式的String类型的数据okhttp 与https
2.利用webView.loadDataWithBaseURL("about:blank",jsonString, "text/html", "utf-8", null);这个方法渲染界面,并展示
程序如下
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.micro_share);
webView = (WebView) this.findViewById(R.id.webView);
webView.setHorizontalScrollBarEnabled(false);
WebSettings setting = webView.getSettings();
setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
// 设置支持javascript
setting.setJavaScriptEnabled(true);
// setting.setUseWideViewPort(true);
// setting.setLoadWithOverviewMode(true);
//
setting.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
setting.setLoadWithOverviewMode(true);
WebViewClient wvClient = new WebViewClient();
webView.setWebViewClient(wvClient);
OkHttpUtils.post().url(tizi_url).build()
.execute(new MyStringCallback());
}
public class MyStringCallback extends StringCallback {
@Override
public void onError(okhttp3.Call call, Exception e) {
// TODO Auto-generated method stub
}
@Override
public void onResponse(String response) {
// jsonString 对应的是html样式的数据
String jsonString = new String(response);
if (jsonString == null || jsonString.equals("")
|| jsonString.equals("null")) {
......
} else {
// 这里用webview渲染web页面
webView.loadDataWithBaseURL("about:blank",jsonString, "text/html", "utf-8", null);
}
}
}