SOAP的接口调用
来源:互联网 发布:35互联域名管理平台 编辑:程序博客网 时间:2024/06/08 07:46
封装好的类:1
package com.goodfamily.gps.polling_and_maintenance.util;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
public class GsonHttpComExchange {
/**
* 静态内部类单例模式(解决了线程同步问题)
*/
private static class SingletonHolder {
private static final GsonHttpComExchange sInstance = new GsonHttpComExchange();
}
public static GsonHttpComExchange getInstance() {
return SingletonHolder.sInstance;
}
private GsonHttpComExchange() {
super();
}
public <T> Respones<T> getReqObject(String obj, Class<T> clazz) {
Respones<T> data = formGson(obj, clazz);
return data;
}
public <T> Respones<T> formGson(String json, Class<T> clazz) {
String reponseBody = null;
Respones<T> resp = new Respones<T>();
T t;
Gson gson = new Gson();
if (json == null || json.equals("")) {
return null;
} else {
try {
JSONObject jsonObject = new JSONObject(json);
Boolean result = jsonObject.getBoolean("IsSucess");
resp.setResult(result);
String message = jsonObject.getString("Message");
resp.setMessage(message);
String dataStr = null;
if (result) {
dataStr = jsonObject.getString("Data");
reponseBody = dataStr;
} else {
return resp;
}
} catch (JSONException e) {
e.printStackTrace();
}
if (String.class == clazz) {
t = (T) reponseBody;
resp.setData(t);
} else {
t = gson.fromJson(reponseBody, clazz);
resp.setData(t);
}
}
return resp;
}
public <T> Object formObjectToClass(Object obj, Class<T> clazz) {
Gson gson = new Gson();
String json = gson.toJson(obj);
return gson.fromJson(json, clazz);
}
public String tojson(Object obj) {
Gson gson = new Gson();
return gson.toJson(obj);
}
public <T> Object formJson(String msg, Class<T> clazz) {
Gson gson = new Gson();
return gson.fromJson(msg, clazz);
}
public <T> Object formJsontoList(String msg, Class<T> clazz) {
if (msg == null || "".equals(msg)) {
return null;
}
Gson gson = new Gson();
T re = gson.fromJson(msg, clazz);
return re;
}
}
封装好的类2
package com.goodfamily.gps.polling_and_maintenance.util;
import java.util.Map;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.goodfamily.gps.entity.LoginData;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class HttpUtill {
public static String nameSpace = "http://116.204.69.57:8081//";// 命名空间
private Handler handler;
private int msgWhat;
private String url;
Map<String, Object> mParam;
SoapObject rpc;
String method;
class PostTread implements Runnable {
@Override
public void run() {
if (null == handler) {
return;
}
Message msg = handler.obtainMessage();
msg.what = msgWhat;
msg.obj = doPost();
handler.handleMessage(msg);
}
}
public void request(Handler handler, SoapObject obj, String method, String url, int msgWhat) {
this.handler = handler;
this.msgWhat = msgWhat;
this.method = method;
this.url=url;
this.rpc = obj;
new Thread(new PostTread()).start();
}
public String doPost() {
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// (new MarshalBase64()).register(envelope);
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(url);
// transport.debug=true;
try {
String soapAction=nameSpace + method;
Log.e("test", soapAction);
// 调用WebService
transport.call(soapAction, envelope);
// transport.call(null, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
if (result == null || result.equals("")) {
result = "{\"IsSucess\":false,\"Message\":\"连接失败\"}";
}
// 将WebService返回的结果显示在TextView中
Log.e("test", result);
return result;
}
}
封装好的类3
package com.goodfamily.gps.polling_and_maintenance.util;
public class Respones<T> {
private Boolean IsSucess;// 返回码
private String Message;// 返回消息
private T Data;// 返回数据
public Boolean getIsSucess() {
return IsSucess;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
public T getData() {
return Data;
}
public void setData(T data) {
Data = data;
}
public void setResult(Boolean IsSucess) {
this.IsSucess = IsSucess;
}
}
application的类
package com.goodfamily.gps.polling_and_maintenance.util;
import java.util.ArrayList;
import java.util.List;
import com.goodfamily.gps.entity.LoginData;
import com.goodfamily.gps.entity.PollingReport;
import com.goodfamily.gps.entity.ReportData;
import android.app.Application;
import android.graphics.Bitmap;
public class MyApp extends Application {
public static GsonHttpComExchange gsonChange;
public static List<PollingReport> list;
private static MyApp instance;
public static MyApp getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
this.instance = instance;
gsonChange = GsonHttpComExchange.getInstance();
list = new ArrayList<PollingReport>();
}
}
登录的例子:
点击登录时调用
public void getRemoteInfo() {
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(HttpUtill.nameSpace, "GetModelByAccount");
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("UserAccount", etUsername.getText().toString());
rpc.addProperty("PassWord", etPassword.getText().toString());
new HttpUtill().request(handler, rpc, "GetModelByAccount",
"http://116.204.69.57:8081/RepairService.asmx?op=GetModelByAccount", 0);
}
- SOAP的接口调用
- 基于apache的soap接口分布调用
- iOS调用SOAP协议的WebServices接口
- 使用soap调用wsdl的接口实战
- thinkphp5 使用soap调用接口
- 如何使用PHP调用ArcGIS Server的Soap接口
- ios 进阶 -- 调用webservice接口(soap协议)的方法
- PHP调用内容DES加密的SOAP接口
- C#的Soap调用
- iOS调用WebService(SOAP接口)
- Javascrpit调用webservice接口(soap方式)
- 关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题
- 如何在其他项目调用webservice 发布的soap风格的接口
- 如何在 SOAP 接口调用的 Http header 中增加字段
- iOS下WebService接口调用与解析-一、Soap对象的封装
- C#调用Server Soap接口MaxArrayLength和MaxStringContentLength错误解决
- python用http发送soap报文进行webservice接口调用
- VC++使用Soap ToolKit3.0调用WebService接口
- CentOS7中使用iptables
- Objective-C Runtime 消息机制
- new String()究竟创建几个对象?
- swift 学习笔记(16)-switch 语句
- centos6.4搭建apache+mysql+php环境
- SOAP的接口调用
- 数据库中函数与存储过程的区别
- Fragment提交transaction导致state loss异常
- 添加Commons11个jar,就ok
- CentOS7中systemctl的使用
- 《Objective-C 高级编程》读书笔记之 ARC
- IBM老机器的引导盘版本对应
- 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛
- 1的个数