WebView.loadData()Demo

     注意点:网络权限
package com.dong.mycameratest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

public class DemoWebViewLoadData extends Activity {
	TextView tv;
    WebView mWebView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.textView);
		mWebView = (WebView) findViewById(R.id.webView1);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		mWebView.setWebChromeClient(new WebChromeClient(){});
		//为了解决网络异常
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()       
        .detectDiskReads()       
        .detectDiskWrites()       
        .detectNetwork()   // or .detectAll() for all detectable problems       
        .penaltyLog()       
        .build());       
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()       
                .detectLeakedSqlLiteObjects()    
                .penaltyLog()       
                .penaltyDeath()       
                .build());  
	}
	public void usePost(View v){
		String uriAPI ="http://www.baidu.com/s";
		HttpPost httpRequest = new HttpPost(uriAPI);//Create Post connection
		//Post must use Array of NameValuePair[] to delay variables.
		List
   
   
    
     params = new ArrayList
    
    
     
     () ;
		params.add(new BasicNameValuePair("wd", "android"));
		
		try {
			//Send Http request
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			// get HttpResponse
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			//Judge state
			if(httpResponse.getStatusLine().getStatusCode() == 200){
				String temp = EntityUtils.toString(httpResponse.getEntity());
//				tv.setText(temp);
				mWebView.loadData(temp, "text/html", HTTP.UTF_8);
			} else {
				tv.setText("Error Response:"+httpResponse.getStatusLine());
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tv.setText(e.getMessage().toString());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tv.setText(e.getMessage().toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tv.setText(e.getMessage().toString());
		}
		
		
	}
	public void useGet(View v){
		
		String uriAPI ="http://www.baidu.com/s?wd=安卓";
		//Create HttpRequest
		HttpGet httpRequGet =new HttpGet(uriAPI);
		try {
			//Get response
			HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequGet);
			
			if(httpResponse.getStatusLine().getStatusCode() == 200){
				String temp = EntityUtils.toString(httpResponse.getEntity());
				mWebView.loadData(temp, "text/html", HTTP.UTF_8);
//				tv.setText(temp);
			} else {
				tv.setText("Error Response:"+httpResponse.getStatusLine());
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block\
			tv.setText(e.getMessage().toString());
			e.printStackTrace();
		} catch (IOException e) {
			tv.setText(e.getMessage().toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

    
    
   
   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值