紧接着上面一个实例:《Android客户端请求服务器端C#的WebService(有源码,调试成功)》来说。Android客户端请求服务器端的WebService之后,WebService获得请求,并对请求做了一些处理。于是服务器要将处理的结果再次返回给客户端。返回的格式有XML和JSON。我们都知道,XML格式的数据处理太复杂,代码量还很多。然而JSON就不同,不仅代码量少,另外还简单,所以我们用JSON数据格式返回给客户端。

首先,介绍一下JSON。

JSON在出现之前,客户端和服务器之间交换的数据格式只有XML。总所周知,XML的生成和解析太麻烦了,没有一种更简单的数据格式来减少程序员的工作量。21世纪初,Douglas Crockford为了解决这个问题,就提出了一种新的数据格式---JSON。

然后说明一下传过来的JSON的数据格式。

Douglas Crockford规定的JSON格式非常简单,他还规定这种格式以后都不会升级,所以掌握了就终生受益。JSON的规则如下:1)并列的数据之间用逗号(“,”)分隔。2)映射用冒号(“:”)表示。3)并列数据的集合(数组)用方括(“[]”)号表示。4)映射的集合(对象)用大括号(“{}”)表示。例如:[ { id:"1",type:"big" },{ id:"2",type:"small"} ]

最后上调试成功的Demo。

User:

public class User implements Serializable{

private static final long serialVersionUID = -758459502806858414L;

private String id;

private String name;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public User(String id, String name) {

super();

this.id = id;

this.name = name;

}

public static List users = new ArrayList();

public User() {}

}

UserList:

package com.fwzkj.tplp2015062501.domain;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

public class LockParkList implements Serializable{

private List userList = new ArrayList();//用于获取服务器返回来的Json数据

//总的车位数

private int usersCount;

public int getUserCount() {

return usersCount;

}

public void setUserCount(int usersCount) {

this.usersCount = lockparksCount;

}

public List getUserList() {

return userList;

}

public void setUserList(List userList) {

this.userList = userList;

}

public UserList() {

super();

}

}

Activity:

/**

* 调用WebService

*

* @return WebService的返回值

*

*/

public String CallWebService( String MethodName, Map Params) {

String soapAction;

if( MethodName == "get_userinfo" ) {

soapAction = GetUserInfo_SOAP_ACTION;

}

System.out.println( "MethodName=" + MethodName );

// 1、新建SoapObject对象,指定webservice的命名空间和调用的webservice方法名

SoapObject request = new SoapObject( Namespace, MethodName );

// 2、设置调用方法的参数值,如果没有参数,可以省略,

if (Params != null) {

Iterator iter = Params.entrySet().iterator();

while ( iter.hasNext() ) {

Map.Entry entry = (Map.Entry) iter.next();

request.addProperty( (String) entry.getKey(), (String) entry.getValue() );

}

}

//2.5、给SoapObject对象添加参数

//request.addProperty("theUserId",id);

//创建HttpTransportSE对象,并指定WebService的WSDL文档的URL

HttpTransportSE ht = new HttpTransportSE(WEB_SERVICE_URL);

//设置debug模式

//ht.debug=true;

//3、生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);//注意是VER11,而不是VER12

//带参数的方法调用

//request.addProperty("id", "1");

//设置bodyOut属性的值为SoapObject对象request

envelope.bodyOut = request;

//指定webservice的类型为dotNet

envelope.dotNet = true;

envelope.setOutputSoapObject(request);

// 使用call方法调用WebService方法

try {

ht.call( soapAction, envelope ); //注意:第一个参数应该对应的SOAP_ACTION,不是空,更不是方法。PS:soapAction可以这样写:Namespace+MethodName

} catch (HttpResponseException e) {

Log.e("----HttpResponseException异常---", e.getMessage());

e.printStackTrace();

} catch (IOException e) {

Log.e("----IOException异常---", e.getMessage());

e.printStackTrace();

} catch (XmlPullParserException e) {

Log.e("----XmlPullParserException异常---", e.getMessage());

e.printStackTrace();

}

try {

//获取返回结果

/**

* SoapObject result = (SoapObject)envelope.bodyIn;

* SoapObject detail = (SoapObject)result.getProperty(METHOD_CheckInLockPark+"Result");

*/

//解析返回的数据信息为SoapObject对象,对其进行解析

/**

* Log.d("huxiubo","detail:"+detail);

* Log.d("huxiubo","0:"+detail.getProperty(0).toString());

* Log.d("huxiubo","1:"+detail.getProperty(1).toString());

*/

if( envelope.getResponse() != null ){

SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

String result = response.toString();

try {

JSONArray json = new JSONArray( result );

UserList userList = new UserList();

userList.setUserCount( json.length() );

for ( int i = 0; i < json.length(); i++ ) {

JSONObject object = json.getJSONObject(i);

user.setId( object.getString("id"));

user.setName(object.getString("name"));

}

} catch (JSONException e) {

e.printStackTrace();

}

//if (result != null) {

//Log.d("----收到的回复----", result.toString());

//return result.toString();

//}

}

} catch (SoapFault e) {

Log.e("----发生错误---", e.getMessage());

e.printStackTrace();

}

return null;

}

运行结果:

0818b9ca8b590ca3270a3433284dd417.png

参考:http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐