之前是用ajax部署和接收webservice 接口数据
那么还有没有其他的方式呢?
例如:已知有一个webservice的接口它返回的是json数据 我们可以通过下面的方式来获取它:
首先要引入
import java.net.HttpURLConnection;
import java.net.URL;
这是来自jre的核心包rt.jar
/**
? * 登录获得access_token
? *
? * @return
? */
?public static String login() {
??String key = "orcde";
??String secret = "oi48jd0uycls876dssa";
??String loginname = "admin";
??String pwd = "123456";
??String GET_URL = "http://com/test/npc/login?key="
????+ key + "&secret=" + secret + "&loginname=" + loginname
????+ "&pwd=" + pwd;
??String res = "";
??try {
???URL getUrl = new URL(GET_URL);
???// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
???// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
???HttpURLConnection connection = (HttpURLConnection) getUrl
?????.openConnection();
???// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
???// 服务器
???connection.connect();
???// 取得输入流,并使用Reader读取
???BufferedReader reader = new BufferedReader(new InputStreamReader(
?????connection.getInputStream()));
???String lines;
???while ((lines = reader.readLine()) != null) {
????res += lines;
???}
???reader.close();
??} catch (Exception e) {
???e.printStackTrace();
??}
??return res;
?}
?
/**
? * 查询登录用户信息
? * @param access_token
? * @param code
? * @return
? */
?public static String getLoginUser(String access_token){
??String GET_URL =" http://com/test/npc/getLogUser?access_token="+access_token;
??String res = "";
??try {
???URL getUrl = new URL(GET_URL);
???// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
???// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
???HttpURLConnection connection = (HttpURLConnection) getUrl
?????.openConnection();
???// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
???// 服务器
???connection.connect();
???// 取得输入流,并使用Reader读取
???BufferedReader reader = new BufferedReader(new InputStreamReader(
?????connection.getInputStream()));
???String lines;
???while ((lines = reader.readLine()) != null) {
????res += lines;
???}
???reader.close();
??} catch (Exception e) {
???e.printStackTrace();
??}
??return res;
?}
这样就得到了接口的数据了。那么怎么对json数据进行处理呢 这个就看需要了
这里根据得到报文的数据创建与报文对应的对象就可以将json转换为对象进而对这个对象进行操作从而简化后面的一些处理。
需要注意的是这里的用到了
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
?
/**
? * 将查询的json 转为javaBean对象
? * @param access_token
? * @return
? */
?public static UserInfo jsonToLoginUser(String access_token){
??UserInfo userInfo=new UserInfo();
??String jsonUser=getLoginUser(access_token);
??String jsonString = jsonUser.substring(jsonUser.indexOf(""data":")+7,
????jsonUser.lastIndexOf("}"));
??JSONObject jsonObject = null;??
??jsonObject = JSONObject.fromObject(jsonString);???
??userInfo =(UserInfo) JSONObject.toBean(jsonObject,UserInfo.class);
??return userInfo;
?}
下面是一个复杂对象,如果这个对象里面包含了一个或多个起塔对象的集合 如下:
部门里面有一个serviceList
像上面那样转的话就会报类型转换错误
/**
? * 将查询的json 转为javaBean
? * @param access_token
? * @param code
? * @return
? */
?public static Org jsonToOrg(String access_token,String code){
Map<String,Class> classMap = new HashMap<String,Class>(); classMap.put("serviceList",Service.class); ??Org org=new Org(); ??String jsonOrg=getOrg(access_token,code); ??String jsonString = jsonOrg.substring(jsonOrg.indexOf(""data":")+15,????jsonOrg.lastIndexOf("}")-1); ??JSONObject jsonObject = null;?? ??jsonObject = JSONObject.fromObject(jsonString);?? ??org =(Org) JSONObject.toBean(jsonObject,Org.class,classMap);//------------如果是复杂类型在这里追加参数classMap ??return org; ?}
这里处理json的方法还不太成熟,应该还有更好的方法下次用到再说吧。