乱码2

package com.witknow.witwebview;

import java.lang.reflect.Method;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.witknow.util.StringUtils;

public class witwebviewViewController extends Activity {
private Handler handler= new Handler();
public WebView myWebView = null;
private Button myButton = null;
private String callbackjsfunc;
@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.witwebview);
myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("GBK");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient()
{
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result)
{
return super.onJsAlert(view, url, message, result);
}

        });
        myWebView.addJavascriptInterface(new WebAppInterface(this),"witwebview");
        myWebView.loadUrl("file:///android_asset/index.html");
        
        // 这里用一个Android按钮按下后调用JS中的代码
        myButton = (Button) findViewById(R.id.button1);
        myButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // 用Android代码调用JavaScript函数:
                //myWebView.loadUrl("javascript:result('asfas')");
            
            }
        }); 
    }
    public class WebAppInterface
    {
        Context mContext;
        WebAppInterface(Context c)
        {
            mContext = c;
        }
        // 如果target 大于等于API 17,则需要加上如下注解
         @JavascriptInterface
        public void postMessage(String json)
        {
             String num = null,classname="",functioname="",params="";
             Class callclass=null;
             Object outputobj=null;
             try
             {
                JSONObject objjson=null;
                objjson = new JSONObject(json);
                classname=objjson.getString("classname");
                functioname=objjson.getString("functionname");
                params = objjson.getString("params");
                callbackjsfunc=objjson.getString("callback");
                JSONObject jsonparams=objjson.getJSONObject("params");//params 为json
             }
             catch(Exception e)
             {
                 Toast.makeText(mContext, "json解析失败"+e.getMessage().toString(), Toast.LENGTH_SHORT).show();
                 return;
             }
             //分析 json  
                try 
                {
                    callclass  = Class.forName("com.witknow.witwebview."+classname);
                    outputobj=callclass.newInstance();//obj指向的A类的对象
                    //Toast.makeText(mContext, "类存在", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                    Toast.makeText(mContext, "类不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                Class[] paramTypes = null;
                try
                {
                    Method[] fields = outputobj.getClass().getDeclaredMethods();//获取对象属性
                    for(int z=0;z

public void returnjs(Object params)
{
myWebView.loadUrl("javascript:result('"+params.toString()+"')");
}
public String output(String str)
{
return str;
}
}

你可能感兴趣的:(乱码2)