注意点:网络权限
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();
}
}
}