1414import java .lang .reflect .InvocationHandler ;
1515import java .lang .reflect .Method ;
1616
17- public class RpcfxInvocationHandler implements InvocationHandler {
17+ public class RpcfxInvocationHandler implements InvocationHandler {
1818
19- public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
19+ public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
2020
21- private final Class <?> serviceClass ;
22- private final String url ;
23- public <T > RpcfxInvocationHandler (Class <T > serviceClass , String url ) {
24- this .serviceClass = serviceClass ;
25- this .url = url ;
26- ParserConfig .getGlobalInstance ().setAutoTypeSupport (true );
27- }
21+ private final Class <?> serviceClass ;
22+ private final String url ;
2823
29- // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
30- // int byte char float double long bool
31- // [], data class
24+ public <T > RpcfxInvocationHandler (Class <T > serviceClass , String url ) {
25+ this .serviceClass = serviceClass ;
26+ this .url = url ;
27+ ParserConfig .getGlobalInstance ().setAutoTypeSupport (true );
28+ }
29+
30+ // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
31+ // int byte char float double long bool
32+ // [], data class
3233
33- @ Override
34- public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
35- return invokePost (method , params );
36- }
34+ @ Override
35+ public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
36+ return invokePost (method , params );
37+ }
3738
39+
40+ /**
41+ * 执行请求
42+ *
43+ * @param method
44+ * @param params
45+ * @return
46+ * @throws IOException
47+ */
3848 private Object invokePost (Method method , Object [] params ) throws IOException {
3949 RpcfxRequest request = new RpcfxRequest ();
4050 request .setServiceClass (this .serviceClass .getName ());
@@ -51,18 +61,18 @@ private Object invokePost(Method method, Object[] params) throws IOException {
5161 }
5262
5363 private RpcfxResponse post (RpcfxRequest req , String url ) throws IOException {
54- String reqJson = JSON .toJSONString (req );
55- System .out .println ("req json: " + reqJson );
64+ String reqJson = JSON .toJSONString (req );
65+ System .out .println ("req json: " + reqJson );
5666
57- // 1.可以复用client
58- // 2.尝试使用httpclient或者netty client
59- OkHttpClient client = new OkHttpClient ();
60- final Request request = new Request .Builder ()
61- .url (url )
62- .post (RequestBody .create (JSONTYPE , reqJson ))
63- .build ();
64- String respJson = client .newCall (request ).execute ().body ().string ();
65- System .out .println ("resp json: " + respJson );
66- return JSON .parseObject (respJson , RpcfxResponse .class );
67- }
68- }
67+ // 1.可以复用client
68+ // 2.尝试使用httpclient或者netty client
69+ OkHttpClient client = new OkHttpClient ();
70+ final Request request = new Request .Builder ()
71+ .url (url )
72+ .post (RequestBody .create (JSONTYPE , reqJson ))
73+ .build ();
74+ String respJson = client .newCall (request ).execute ().body ().string ();
75+ System .out .println ("resp json: " + respJson );
76+ return JSON .parseObject (respJson , RpcfxResponse .class );
77+ }
78+ }
0 commit comments