package com.bwie.day10_webview_use;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView wv;
Context con;
private String data;
private static final String ENDURL="http://192.168.23.1:8080/Day04_urlCon/urlServerlet";
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
//接受数据
String ii=msg.obj.toString().trim();//去空格
System.out.println(ii);
if(ii.equals("success"))
{
Intent intent=new Intent(MainActivity.this,NewActivity.class);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "不存在", 0).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con=this;
// 初始化数据
init();
}
private void init() {
// 找到控件
wv = (WebView) findViewById(R.id.wv);
// 获取websettings对象(一。设置编码格 式 二。支持JavaScript)
WebSettings settings = wv.getSettings();
// 设置编码格式
settings.setDefaultTextEncodingName("utf-8");
// 支持js
settings.setJavaScriptEnabled(true);//必须得设置不然js代码不能实现
// 加载html(本地的)
wv.loadUrl("file:///android_asset/new_file.html");
//解决js中的alert,confirm无效的问题
wv.setWebChromeClient(new WebChromeClient() {
// 重写alert
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// 关闭系统自带的
result.cancel();
// 创建Android中的alert
AlertDialog.Builder b = new AlertDialog.Builder(
MainActivity.this);
b.setTitle("提示框").setIcon(R.drawable.ic_launcher)
.setMessage(message).setPositiveButton("确定", null)
.show();
return true;// 修改成功
}
// 重写confirm
@Override
public boolean onJsConfirm(WebView view, String url,
String message, final JsResult result) {
// 创建Android中的alert(注意不能关闭系统的提交框)
AlertDialog.Builder b = new AlertDialog.Builder(
MainActivity.this);
b.setTitle("确认框")
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
result.confirm();// 提交系统的确认框
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
result.cancel();//取消系统的确认框
}
}).show();
return true;// 修改成功
}
});
//向js中倒入一个对象,进行两者之间的通信
wv.addJavascriptInterface(new MyObjext(), "myObject");
}
class MyObjext
{
@JavascriptInterface//一定要记得重写
public void sendData(final String name,final String pass,final String email)
{
Toast.makeText(con, name+","+pass+","+email, 1).show();
//将HTML值添加到服务器中
new Thread(){
public void run() {
String http = getDataByHttp(ENDURL,name,pass);
//向主线程发送消息
handler.sendMessage(handler.obtainMessage(1, http));
};
}.start();
}
}
//进行登录验证
public String getDataByHttp(String url,String name,String pass)
{
StringBuffer sb=new StringBuffer(url);
sb.append("?username="+name);
sb.append("&userpass="+pass);
//获取get请求对象
HttpGet get=new HttpGet(sb.toString());
//设置请求参数
HttpParams params=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
//获取执行请求的对象
HttpClient client=new DefaultHttpClient(params);
//执行请求
try {
HttpResponse resp = client.execute(get);
//判断是否响应成功
if(resp.getStatusLine().getStatusCode() == 200)
{
//获取响应的数据
HttpEntity entity = resp.getEntity();
//将数据强转为字符串
data = EntityUtils.toString(entity, "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}