乱码2

作者: cvcphp | 来源:发表于2016-08-24 10:58 被阅读0次

    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<fields.length;z++)
                        {
                            Method field=fields[z];
                            if(field.getName().toString().equals("output"))
                              {
                                paramTypes = field.getParameterTypes();//获得一个方法参数数组(getparameterTypes用于返回一个描述参数类型的Class对象数组)
                                break;
                                //for(int j = 0 ; j < paramTypes.length; j++)
                                //{
                                    //Toast.makeText(getApplicationContext(), paramTypes[j].getName().toString(), Toast.LENGTH_SHORT).show();
                                    //Toast.makeText(mContext,paramTypes[ j ].getName(), Toast.LENGTH_SHORT).show();
                                //}
                                 //break;
                              }
                        }
                        Method method = callclass.getMethod("output",paramTypes);//初始化方法
                        final Object obj=method.invoke(outputobj,"dfgHJKL:FGHJKL:");
                        handler.post(new Runnable() { 
                            @Override 
                            public void run(){ 
                                returnjs(obj);
                            } 
                        });
                        
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(mContext, "方法不存在"+e.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        return;
                    }
            }
         
        }
    

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

    相关文章

      网友评论

          本文标题:乱码2

          本文链接:https://www.haomeiwen.com/subject/xbansttx.html