Android的xml-Rpc实现
弄Rpc有一段时间了,从定义IDL规则,自动生成存根代码给应用使用,相继做了c++,java,python,actionscript四个版本,且都都能互相调用,协议是自定义的二进制格式。
公司的项目要求我从事android的开发,编写一些小的应用,要与平台服务器通信。
与服务器的通信采用xml交换,之前做的二进制编码便不能使用。
不想很弱智的去重复的编解码xml消息格式,去编写socket通信代码,工作量和繁琐是自己不喜欢的,那只能让自己的rpc去支持xml格式。 www.zzzyk.com
这种支持我称之为半人工的,因为xml格式的tag定义必须在idl中出现
idl定义
1 struct Ts_heartbeat_p_User_t{
2 string id;
3 };
4 //心跳
5
6 struct Ts_gps_p_GPS_t{
7 float lon;
8 float lat;
9 float speed;
10 float direction;
11 int time;
12 };
13
14 struct Ts_gps_p_Properties_t{
15 string senderid;
16 string target_type;
17 string users;
18 string groups;
19 };
20
21 struct Ts_verify_r_User_t{
22 string id;
23 };
24
25 struct Ts_verify_r_Group_t{
26 string id;
27 string name;
28 string type;
29 };
30
31 sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t;
32
33 struct Ts_verify_r_Result_t{
34 int code;
35 string msg;
36 Ts_verify_r_User_t user;
37 Ts_verify_r_Groups_t groups;
38 };
39
40
41 struct Ts_verify_p_User_t{
42 string token;
43 };
44
45 inte易做图ce Ts{
46 void heartbeat(Ts_heartbeat_p_User_t p);
47 void gps(Ts_gps_p_GPS_t p,Ts_gps_p_Properties_t props);
48 Ts_verify_r_Result_t verify(Ts_verify_p_User_t p);
49 };
50
idl定义了一个Ts的接口,内部有三个方法:heartbeat(),gps(),verify()
这些方法在服务器端已经实现,其他的struct类型定义了接口调用使用的参数和返回值类型 ,注意他们的格式:
struct Ts_verify_p_User_t{
string token;
};
这个类型将被解释成 <User token="..."/>
sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t;
将被解释成
<Groups>
<Group id="" name="" type=""/>
...
</Groups>
努力不是白费的,python+ply实现了idl的语法解析,并生成java框架代码,并编写通信代码完成Rpc构造。
必须要注意Android不允许任何阻塞UI线程的操作,所以要实现通信必须在自己的线程中进行,我提供的Rpc调用模式只能是 oneway(单向调用) ,async(异步调用)
实例:
1 package test;
2
3
4 import sns_mobile.*;
5
6 import java.util.*;
7 import tce.*;
8
9 import java.nio.*;
10 import java.io.*;
11
12 public class SnsTestMain {
13 TsProxy tsprx = null;
14 String token="dBmPPfxxohFmXjx4SUv46BffzmeYitbjmzT2mRHLttGaau4kiTvKEOpW2BJE0IGLEBFZ4lE/KrMKl7qE6pNmaBUfbixMrPEG/1Xfsp36P4lmZHhQfkVgyGWa8yi5g/swbMpg8h2ybC2dQShR18VFtQkXGpEUg8VfV1Fb0UllN6j/umVaBBQX7lgqcf3zkIaGcOpvS7FB6snC/PWU1crCf2gHrgnOmZgIaKmBF14dkJsSoozRGKiIpiCjJ7mRZ05og67ZmFfOaH2EnaCo3G1pmQcez7MOqCtIvNpGo5yivmZ6GyMA2eSIlFmD2Z7ysCrxzAt92UUCNwb9mkKZrUthjA==";
15
16 double locs[]={436914.309600,112259.854800,436914.309600,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.291600,112259.844000,436914.288000,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.306000,112259.836800,436914.309600,112259.836800,436914.316800,112259.826000,436914.324000,112259.811600,436914.334800,112259.793600,436914.345600,112259.775600,436914.352800,112259.775600,436914.360000,112259.775600,436914.370800,112259.764800,436914.378000,112259.757600,436914.381600,112259.739600,436914.381600,112259.754000,436914.381600,112259.808000,436914.352800,112259.883600,436914.316800,112260.024000,436914.280800,112260.186000,436914.244800,112260.376800,436914.234000,112260.600000,436914.201600,112260.834000,436914.165600,112261.086000,436914.118800,112261.359600,436914.075600,112261.654800,436914.036000,112261.942800,436914.000000,112262.230800,436913.910000,112262.562000,436913.841600,112262.907600,436913.794800,112263.285600,436913.733600,112263.660000,436913.676000,112264.038000,436913.614800,112264.401600,436913.553600,112264.776000,436913.517600,112265.128800,436913.445600,112265.463600,436913.373600,112265.805600,436913.334000,112266.180000,436913.290800,112266.540000,436913.244000,112266.910800,436913.244000,112266.910800,436913.139600,112267.695600,436913.139600,112267.695600,436913.139600,112267.695600,436913.056800,112268.440800,436913.002800,112268.793600,436912.923600,112269.081600,436912.920000,112269.276000,436912.938000,112269.376800,436912.995600,112269.369600,436913.013600,112269.366000,436913.020800,112269.394800,436913.013600,112269.423600,436913.010000,112269.441600,436913.020800,112269.459600,436913.010000,112269.495600,436913.010000,112269.513600,436913.002800,112269.531600,436912.992000,112269.546000,436912.977600,112269.564000,436912.974000,112269.567600,436912.966800,112269.582000,436912.956000,112269.585600,436912.948800,112269.592800,436912.941600,112269.600000,436912.938000,112269.610800,436912.930800,112269.618000,436912.923600,112269.621600,436912.923600,112269.636000,436912.923600,112269.639600,436912.923600,112269.639600,436912.923600,112269.639600,436912.920000,112269.646800,436912.920000,112269.654000,436912.920000,112269.657600,436912.912800,112269.664800,436912.920000,112269.664800,436912.912800,112269.672000,436912.912800,112269.675600,436912.912800,112269.682800,436912.912800,112269.693600,436912.912800,112269.693600,436912.905600,112269.700800,436912.905600,112269.700800,436912.905600,112269.700800,436912.902000,112269.711600,436912.902000,112269.711600,436912.902000,112269.726000,436912.894800,112269.736800,436912.894800,112269.747600,436912.887600,112269.772800,436912.884000,112269.801600,436912.884000,112269.801600,436912.858800,112269.852000,436912.851600,112269.888000,436912.833600,112269.924000,436912.815600,112269.963600,436912.797600,112270.032000,436912.768800,112270.104000,436912.768800,112270.104000,436912.707600,112270.500000,436912.660800,112270.719600,436912.617600,112270.942800,436912.560000,112271.205600,436912.527600,112271.508000,436912.455600,112271.824800,436912.372800,112272.148800,436912.311600,112272.498000,436912.236000,112272.850800,436912.149600,112273.218000,436912.059600,112273.588800,436911.966000,112273.959600,436911.861600,112274.344800,436911.753600,112274.715600,436911.642000,112275.100800,436911.642000,112275.100800,436911.346800,112275.838800,436911.202800,112276.173600,436911.051600,112276.476000,436910.896800,112276.785600,436910.860800,112277.055600,436910.796000,112277.271600,436910.727600,112277.469600,436910.670000,112277.620800,436910.634000,112277.721600,436910.644800,112277.746800,436910.619600,112277.757600,436910.616000,112277.764800,436910.616000,112277.764800,436910.619600,112277.782800,436910.616000,112277.782800,436910.616000,112277.782800,436910.608800,112277.775600,436910.608800,112277.772000,436910.608800,112277.772000,436910.608800,112277.772000,436910.601600,112277.7
补充:移动开发 , Android ,