@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); myProgressDialog2 = new MyProgressDialog(this); myProgressDialog2.setMessage("正在请求..."); myProgressDialog2.show(); context = this; intent = getIntent(); loadUrl = intent.getStringExtra("loadUrl"); bt_update = (Button) findViewById(R.id.id_bt_update); // commonActivityTopView = (CommonActivityTopView) // findViewById(R.id.id_CommonActivityTopView); // commonActivityTopView.tv_title.setText("webview"); rl = (RelativeLayout) findViewById(R.id.id_rl); loadWebView(); webViewListener(); initListener(); LogUtil.i(TAG, SharedPrefsUtil.getValue(Constants.TOKEN, null)); HashMap<String, String> hashmap = new HashMap<String, String>(); if (null != SharedPrefsUtil.getValue(Constants.TOKEN, null)) { hashmap.put("token", SharedPrefsUtil.getValue(Constants.TOKEN, null)); } webview.loadUrl(loadUrl, hashmap); }
@SuppressLint({"NewApi", "SetJavaScriptEnabled"}) private void loadWebView() { // 实例化WebView对象 // webview = new MyWebView(WebViewActivity.this); LogUtil.i(TAG, "loadWebView===实例化WebView==="); webview = (MyWebView) findViewById(R.id.id_webview); new MobclickAgentJSInterface(this, webview); WebSettings webSettings = webview.getSettings(); // 设置WebView属性,能够执行Javascript脚本 webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); // webSettings.setUseWideViewPort(true); // 关键点 webSettings.setLoadWithOverviewMode(true); webSettings.setAppCacheEnabled(true); webSettings.setAppCacheMaxSize(8 * 1024 * 1024); // 8MB // webSettings.setAppCachePath(Constants.WEBVIEW_CACHE_DIR ); String appCacheDir = this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); webSettings.setAppCachePath(appCacheDir); webSettings.setAllowFileAccess(true); webSettings.setDomStorageEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // js调用安卓方法 webview.addJavascriptInterface(this, "RedirectListner"); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { LogUtil.i(TAG, "==onKeyDown=="); if (isPageLoaded) { webview.loadUrl("javascript:CommonRedirect.goBack()"); LogUtil.i(TAG, "==onKeyDown==isPageLoaded=="); } else { if (webview.canGoBack()) { webview.goBack(); } else { finish(); } } } return false; }
/** 将修改后的信息上传服务器 */ public void requestModifyInfo() { try { // 向服务器发送请求 // RequestParams params = new RequestParams(); // params.addHeader(Constants.TOKEN, // SharedPrefsUtil.getValue(Constants.TOKEN, "")); JSONObject jsonParam = new JSONObject(); jsonParam.put("photo", ""); jsonParam.put("fileName", ""); String userJson = SharedPrefsUtil.getValue(Constants.USERMODEL, ""); UserModel userModel = new Gson().fromJson(userJson, UserModel.class); // String userSex = ""; // if(userModel.getUserSex().equals("男")){ // userSex = "Man"; // }else if(userModel.getUserSex().equals("女")){ // userSex ="Woman"; // } jsonParam.put("sex", userModel.getUserSex()); if (title.equals("姓名")) { jsonParam.put("Name", et.getText().toString().trim()); jsonParam.put("IDcard", userModel.getIDcard()); jsonParam.put("UserAccount", userModel.getUserAccount()); } else if (title.equals("身份证号")) { jsonParam.put("Name", userModel.getUserName()); jsonParam.put("IDcard", et.getText().toString().trim()); jsonParam.put("UserAccount", userModel.getUserAccount()); } else { jsonParam.put("Name", userModel.getUserName()); jsonParam.put("IDcard", userModel.getIDcard()); jsonParam.put("UserAccount", et.getText().toString().trim()); } // params.addBodyParameter("param", jsonParam.toString()); LogUtil.i("=====", "===" + userModel.getUserSex() + "==="); LogUtil.i("=====", jsonParam.toString()); // params.setBodyEntity(new StringEntity(jsonParam.toString())); // HttpUtils httpUtils = new HttpUtils(); HttpClient.post( Constants.MODIFYINFO_URl, jsonParam.toString(), new RequestCallBack<String>() { @Override public void onStart() { Log.i("HttpUtil", "onStart"); } @Override public void onLoading(long total, long current, boolean isUploading) { Log.i("HttpUtil", "onLoading"); } @Override public void onSuccess(ResponseInfo<String> responseInfo) { Log.i("HttpUtil", "onSuccess"); Log.i("HttpUtil", "onSuccess===" + responseInfo.result.toString()); ToastUtil.showMessage("修改成功"); // 存本地 UserModel userModel = new Gson() .fromJson(SharedPrefsUtil.getValue(Constants.USERMODEL, ""), UserModel.class); if (title.equals("姓名")) { // 存入修改后的姓名 userModel.setUserName(et.getText().toString().trim()); } else if (title.equals("身份证号")) { // 存入修改后的身份证号 userModel.setIDcard(et.getText().toString().trim()); // 存入年龄 userModel.setAge(MathUtils.calculateAge(et.getText().toString().trim())); // SharedPrefsUtil.putValue(Constants.USERMODEL, // userModel.toString()); } else { // 存入修改后的手机号 // userModel.setMobilephone(et.getText().toString() // .trim()); } SharedPrefsUtil.putValue(Constants.USERMODEL, userModel.toString()); Log.i("HttpUtil", "存入手机"); finish(); } @Override public void onFailure(HttpException error, String msg) { Log.i("HttpUtil", "onFailure===" + msg); LogUtil.i("===============", error.toString()); ToastUtil.showMessage("网络获取失败"); LogUtil.i("=================", Constants.MODIFYINFO_URl); // testTextView.setText(error.getExceptionCode() + // ":" + // msg); } }); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }