Android Development with phonegap and odata 以及与JS 交互

来源:互联网 发布:淘宝网注册开店 编辑:程序博客网 时间:2024/05/16 19:20

最近搞微信安卓开发,挺好玩的。

难点有几个:

1. 必须利用phonegap,因为公司的UI都是基于HTML5的。

2. 利用phonegap拿到公司的Odata,有权限问题

3. 利用HTML5的控件触发JAVA程序

4. 触发JAVA程序之后还要传值进去,不然也没用

5. 传值进去之后,还要在Activity之间传值,最后调用发送到微信的程序,自动填入想要发送的内容


另外难点还有在微信上注册代码,MD5码之类的就不说了。

1. 如何解决,直接上Phonegap官网看教程,很简单,不详述

2. 由于不能像浏览器那样点击确定,只能用内部验证。

  有两种方法

    一:

    直接写url然后再内面加入用户名和密码的请求。

sUrl = "path";//Odata request url$.ajax({  type: "GET",  url:sUrl,    dataType: 'json',    timeout: 10000,    beforeSend: function (req) {        req.setRequestHeader('Authorization', make_base_auth('username', 'password'));    },    success: function (data, textStatus, response) {        console.log (data.d.results[0]['AlertCate']);//My process code        showAlerts (data.d.results);    },    error: function (data, textStatus, response) {      console.log ("fail");    }});function make_base_auth(user, password) {    var tok = user + ':' + password;    var hash = btoa(tok);    return 'Basic ' + hash;}

   二, 在前面加上 "username:password@url" 也行的。

sUrl = "username:password@path";//Odata request url$.ajax({  type: "GET",  url:sUrl,    dataType: 'json',    timeout: 10000,    success: function (data, textStatus, response) {        console.log (data.d.results[0]['AlertCate']);//My process code        showAlerts (data.d.results);    },    error: function (data, textStatus, response) {      console.log ("fail");    }});function make_base_auth(user, password) {    var tok = user + ':' + password;    var hash = btoa(tok);    return 'Basic ' + hash;}

3. 触发JAVA程序其实挺简单的

   第一,在你要用到的JAVA类中加入

    private DroidGap mGap;    public Weixin (DroidGap gap)    {      mGap = gap;    }
以类Weixin.class 为例,加入后变成:

public class Weixin extends Activity {private DroidGap mGap;public Weixin (DroidGap gap)    {      mGap = gap;    }    public void send (String MSG) {     System.out.println(MSG);    }}

 第二,在调用到Phonegap界面的类中(一般是mainActivity),加入:

private Weixin weixinPlugin;
以及在onCreate的方法中加入

super.init();weixinPlugin = new Weixin (this);appView.getSettings().setJavaScriptEnabled(true);appView.addJavascriptInterface(weixinPlugin, "weixinPlugin");

最终结果是:

public class MainActivity extends DroidGap {private Weixin weixinPlugin;@SuppressLint("SetJavaScriptEnabled")@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.init();weixinPlugin = new Weixin (this);appView.getSettings().setJavaScriptEnabled(true);appView.addJavascriptInterface(weixinPlugin, "weixinPlugin");super.loadUrl("file:///android_asset/www/index.html");}}

最后,在HTML里面的JS进行调用:

window.weixinPlugin.send('From the front end');
结果就会在Log里面输出 ‘From the front end'.


4. 至于传值,在上面已经写进去了。只要把变量同事加到JS和JAVA方法里面就行了。

    值得注意的是,千万别把JS的方法和JAVA的方法用法弄混了,绝对不能写:

var method = window.weixinPlugin.send('From the front end');
方法是从JAVA里面拿过来的,不能用JS的处理方法对待,他不能作为对象来进行操作。

5. 在Activity之间传值和普通的传值有点区别(在Send方法里面):

         Intent myIntent = new Intent (mGap, SendToWXActivity.class);//这个地方是第三点定义的mGap,不是this,或者别的activity,实际上是mainActivity         System.out.println(MSG);         myIntent.putExtra("TextId", MSG);     mGap.startActivity (myIntent);

调用就简单了

Bundle b = getIntent().getExtras();String textString = b.getString("TextId");

我发现在主activity中,system.out.print是无效的。我试了很多方法发现只能用Log.w () , 原因暂时不深究:

Log.w("Text Content", textString);

最终要注意的是,HTML传值到JAVA类,只能传入Activity或者是DroidGap类中,就是要extends Activity或者是extends DroidGap,否则只用普通类值是传不进去的。



0 0