当前位置:   article > 正文

WebView通过网络请求加载html_qwebview必须要联网才能读取html文件吗

qwebview必须要联网才能读取html文件吗

 步骤:
 1.正常进行网络请求,
2. 正常解析json(得到解析好的String s)
3. oncreat方法中设置webView的wv.getSettings().setJavaScriptEnabled(true);
 4.获得以上资源后,调用webview的loadDataWithBaseURL(url, s, "text/html", "utf-8", null);


xml文件中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
     <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/wv" />
</LinearLayout>



代码


public class TwoActivity extends Activity {
    private WebView webView;
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                String str = (String) msg.obj;
                //解析json
                String s = jieJson(str);
                //在webView里显示
                initWebView(s);
                break;

            default:
                break;
            }
        };
    };
    private String url = "http://www.93.gov.cn/93app/info.do?id=10944805";

    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        webView= (WebView) findViewById(R.id.wv);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //网络请求数据
        initHttp();
    }
    
    //网络请求数据
    public void initHttp(){
        new Thread(){
            public void run() {
                try {
                    //获得HttpClient对象
                    HttpClient client = new DefaultHttpClient();
                    HttpGet get = new HttpGet(url);
                    HttpResponse response = client.execute(get);
                    StatusLine line = response.getStatusLine();
                    int code = line.getStatusCode();
                    if(code == 200){
                        HttpEntity entity = response.getEntity();
                        String str = EntityUtils.toString(entity, "utf-8");
                        Message message = Message.obtain();
                        message.obj = str;
                        message.what = 0;
                        handler.sendMessage(message);;
                    }
                    
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }

    //解析json
    public String jieJson(String str){
        try {
            JSONObject jo = new JSONObject(str);
            String context = jo.getString("contenttext");
            return context;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    //在webView里显示
    public void initWebView(String s){
        webView.loadDataWithBaseURL(url, s, "text/html", "utf-8", null);
    }

}




声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号