余生_Mrs
//开启启动页,布局加个WebView,并设置本应用加载WebView。加载完后,下面是Fragment加载的WebViewpackage com.example.dongdong_frist_app.fragment;import java.io.IOException;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.util.EntityUtils;import android.annotation.SuppressLint;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.webkit.JavascriptInterface;import android.webkit.JsResult;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Toast;import com.example.dongdong_frist_app.R;import com.example.dongdong_frist_app.dao.BuildDataDao;@SuppressLint("ShowToast") public class Login_Fragment extends Fragment { private View view; private WebView wv; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.login_page, null); new Thread(){ public void run() { init(); }; }.start(); return view; } public void init(){ wv = (WebView)view.findViewById(R.id.wv); wv.loadUrl("http://169.254.238.190:8081/Dongdong_Test2/html5-step-by-step-reg-form/login.html"); //获得WebSetting,设置js有效 WebSettings setting = wv.getSettings(); setting.setJavaScriptEnabled(true); //把WebViewHelper的对象,传递到js中,供调用 wv.addJavascriptInterface(new WebViewHelper(), "wvHelper"); wv.setWebChromeClient(new WebChromeClient(){ @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { // TODO Auto-generated method stub return super.onJsAlert(view, url, message, result); } }); } //webview的帮助类 class WebViewHelper{ //让webview加载注册界面 BuildDataDao dao = new BuildDataDao(getActivity()); @JavascriptInterface public void goToRegist(){ wv.loadUrl("http://169.254.238.190:8081/Dongdong_Test2/html5-step-by-step-reg-form/index.html"); } @JavascriptInterface public void sendData(String name,String pass){ Log.i("aaa", "sendData=="+name+pass); Toast.makeText(getActivity(), "====="+name+""+pass, 0).show(); //将用户注册信息保存到服务器 String url = "http://169.254.238.190:8081/Dongdong_Test2/AddMessage?username="+name+"&password="+pass+""; String index = httpUtiles(url); if(index.equals("true")){ Toast.makeText(getActivity(), "您已经注册成功", 0).show(); //跳转到登陆界面 wv.loadUrl("http://169.254.238.190:8081/Dongdong_Test2/html5-step-by-step-reg-form/login.html"); }else if(index.equals("false")){ Toast.makeText(getActivity(), "您的输入有误,请重新输入", 0).show(); wv.loadUrl("http://169.254.238.190:8081/Dongdong_Test2/html5-step-by-step-reg-form/index.html"); } } @JavascriptInterface public void sendUserMSG(String name,String pass){ Log.i("aaa", "sendUserMSG=="+name+pass); //将用户信息提交到服务器进行校验 String url = "http://169.254.238.190:8081/Dongdong_Test2/IsCheckedServlet?username="+name+"&password="+pass+""; String index = httpUtiles(url); if(index.equals("true")){ Toast.makeText(getActivity(), "登录成功", 1).show(); wv.destroy(); //获取Fragment的管理器登陆到主页面 FragmentManager fm = getActivity().getSupportFragmentManager(); HomePagerFragmnet homepagerfragment = new HomePagerFragmnet(); fm.beginTransaction().replace(R.id.wv, homepagerfragment).commit(); }else if(index.equals("false")){ Toast.makeText(getActivity(), "您的输入有误,请重新输入", 0).show(); wv.loadUrl("http://169.254.238.190:8081/Dongdong_Test2/html5-step-by-step-reg-form/login.html"); } } } public String httpUtiles(String url) { String str = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(url)); if(response.getStatusLine().getStatusCode() == 200){ str = EntityUtils.toString(response.getEntity(), "utf-8"); Log.i("aaa", ""+str); return str; } } catch (IOException e) { e.printStackTrace(); } return null; } }