android 模拟浏览器访问网络

android 模拟浏览器访问网络

package com.cnten.zentest;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.cnten.dao.NetUtil;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.EditText;
public class NetCnten extends Activity implements OnClickListener{


private EditText urlText;
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.netcnten);
urlText = (EditText)findViewById(R.id.etUrl);
webView = (WebView) findViewById(R.id.etContent);
findViewById(R.id.btnGo).setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if(id==R.id.btnGo){
String urlStr = urlText.getText().toString();
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
webView.loadDataWithBaseURL(null,NetUtil.readDataAsString(is),"text/html","utf-8",null);
}
} catch (Exception e) {
Log.i("异常>>>>>", e.getMessage());
}


}

}

}

android 模拟浏览器访问网络