首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

phprpc联接java与flex的简单实例

2012-11-05 
phprpc连接java与flex的简单实例在网上听说了phprpc,呵呵,于是就想用其来连接java与flex,本人对代码的感知

phprpc连接java与flex的简单实例
在网上听说了phprpc,呵呵,于是就想用其来连接java与flex,本人对代码的感知能力比较差,在携剑看山,andot,缘起缘灭,网络伟伟等牛人的指导下,终于完成了第一个事例,在此特表感谢!说来惭愧,看phprpc已经看了两三天了,唉……
好了,下面是代码:

PHPRPC服务器端:
建处servlet,我取名为ComputeServlet.java
Java代码 复制代码

   1. package com.rsrt.test;  
   2.  
   3. import java.io.IOException;  
   4. import java.io.PrintWriter;  
   5.  
   6. import javax.servlet.ServletException;  
   7. import javax.servlet.http.HttpServlet;  
   8. import javax.servlet.http.HttpServletRequest;  
   9. import javax.servlet.http.HttpServletResponse;  
  10. import org.phprpc.PHPRPC_Server;  
  11.  
  12. public class ComputeServlet extends HttpServlet {  
  13.  
  14.       
  15.     public ComputeServlet() {  
  16.         super();  
  17.     }  
  18.  
  19.       
  20.     public void destroy() {  
  21.         super.destroy();  
  22.           
  23.     }  
  24.  
  25.       
  26.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  27.             throws ServletException, IOException {  
  28.           
  29.         this.doPost(request, response);  
  30.  
  31.       
  32.     }  
  33.  
  34.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  35.             throws ServletException, IOException {  
  36.  
  37.         PHPRPC_Server ps = new PHPRPC_Server();  
  38.         SaySomething ss = new SaySomething();  
  39.         ps.add("SayHi",ss);  
  40.         ps.add("SayGoodbye",ss);  
  41.         ps.start(request, response);  
  42.     }  
  43.  
  44.  
  45.     public void init() throws ServletException {  
  46.           
  47.           
  48.     }  
  49.  
  50. } 

package com.rsrt.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.phprpc.PHPRPC_Server;

public class ComputeServlet extends HttpServlet {


public ComputeServlet() {
  super();
}


public void destroy() {
  super.destroy();
 
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  this.doPost(request, response);


}

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  PHPRPC_Server ps = new PHPRPC_Server();
  SaySomething ss = new SaySomething();
  ps.add("SayHi",ss);
  ps.add("SayGoodbye",ss);
  ps.start(request, response);
}


public void init() throws ServletException {
 
 
}

}



工具类:

1)先定义一个接口:IUpload.java
Java代码 复制代码

   1. package com.rsrt.test;  
   2.  
   3. import java.io.*;  
   4.  
   5. public interface IUpload {  
   6.  
   7.     public String SayHi(String to);  
   8.     public String SayGoodbye(String to);  
   9. } 

package com.rsrt.test;

import java.io.*;

public interface IUpload {

public String SayHi(String to);
public String SayGoodbye(String to);
}



2)实现接口:SaySomething.java
Java代码 复制代码

   1. package com.rsrt.test;  
   2.  
   3. public class SaySomething implements IUpload {  
   4.       
   5.     public String SayHi(String to)  
   6.     {  
   7.         System.out.print("HI,"+to);  
   8.         return "HI,"+to;  
   9.     }  
  10.       
  11.     public String SayGoodbye(String to)  
  12.     {  
  13.         System.out.println("SayGoodbye,"+to);  
  14.         return "Goodbye,"+to;  
  15.     }  
  16.  
  17. } 

package com.rsrt.test;

public class SaySomething implements IUpload {

public String SayHi(String to)
{
  System.out.print("HI,"+to);
  return "HI,"+to;
}

public String SayGoodbye(String to)
{
  System.out.println("SayGoodbye,"+to);
  return "Goodbye,"+to;
}

}



3)java客户端:UploadClient .java
Java代码 复制代码

   1. package com.rsrt.test;  
   2. import org.phprpc.*;  
   3.  
   4. public class UploadClient {  
   5.  
   6.     public static void main(String[] args) {  
   7.           
   8.  
   9.         PHPRPC_Client client = new PHPRPC_Client("http://localhost:8080/PhprpcAs/servlet/ComputeServlet");  
  10.         //注:此处用的是接口,不是类。  
  11.         IUpload upload = (IUpload)client.useService(IUpload.class);  
  12.         String hi = upload.SayHi("nishiting");  
  13.         String gb = upload.SayGoodbye("nishiting");  
  14.         System.out.println(hi);  
  15.         System.out.println(gb);  
  16.     }  
  17.  
  18. } 

package com.rsrt.test;
import org.phprpc.*;

public class UploadClient {

public static void main(String[] args) {
 

  PHPRPC_Client client = new PHPRPC_Client("http://localhost:8080/PhprpcAs/servlet/ComputeServlet");
  //注:此处用的是接口,不是类。
  IUpload upload = (IUpload)client.useService(IUpload.class);
  String hi = upload.SayHi("nishiting");
  String gb = upload.SayGoodbye("nishiting");
  System.out.println(hi);
  System.out.println(gb);
}

}



4)flex的客户端:xxx.mxml
Java代码 复制代码

   1. "1.0" encoding="utf-8"?>  
   2. "http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientColors="[0x000000,0x323232]"  fontSize="16" creationComplete="init()">  
   3.  
   4.  
   5.       
   6.        
   7.                 client.SayHi("http://www.y-city.net.cn",function (result:*,args:Array,output:String,warning:PHPRPC_Error):void 
   8.                 {  
   9.                       
  10.                 hiTA.text += result.toString();  
  11.                 },true);  
  12.                   
  13.                 client.SayGoodbye("goto http://www.y-city.net.cn",function (result:*,args:Array,output:String,warning:PHPRPC_Error):void 
  14.                 {  
  15.                       
  16.                 gbTA.text += result.toString();  
  17.                 },true);  
  18.                   
  19.             }  
  20.         ]]>  
  21.       
  22.       
  23.     "100%" height="100%" >  
  24.       
  25.         "secondContainer" borderAlpha="0.15" color="0xffffff" title="测试PHPRPC" horizontalCenter="0" verticalCenter="022">  
  26.             "0x323232">  
  27.                 "客户端取到的值">  
  28.                     "hiTA" width="300"/>  
  29.                     "gbTA" width="300"/>  
  30.                   
  31.               
  32.           
  33.           
  34.       
  35.       
  36.  



 




 
  
   
    
    
   
  
 
 




  要注意的有几点:

1)非静态方法服务器加载其类时不能直接用 xx.class,而要加载其实例。如SaySomething ss = new SaySomething();ps.add("SayHi",ss);

2)客户端中IUpload upload = (IUpload)client.useService(IUpload.class);要用接口而不是类。

热点排行