All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mobi.cangol.mobile.soap.SoapClient Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
/**
 * Copyright (c) 2013 Cangol.
 * 

* Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mobi.cangol.mobile.soap; import android.content.Context; import android.util.Log; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.kxml2.kdom.Element; import org.kxml2.kdom.Node; import java.lang.ref.WeakReference; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.Future; import mobi.cangol.mobile.service.PoolManager; /** * SoapClient.java 使用此类需要ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar * * @author Cangol */ public class SoapClient { private static final String TAG = "SoapRequest"; private static final int TIMEOUT = 20 * 1000; private final Map>>> requestMap; private SoapSerializationEnvelope envelope; private PoolManager.Pool threadPool; public SoapClient() { threadPool = PoolManager.buildPool(TAG, 3); requestMap = new WeakHashMap<>(); envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.dotNet = true; } /** * 添加header * * @param namespace * @param authheader * @param headers */ public void addHeader(String namespace, String authheader, Map headers) { if (authheader != null && headers != null) { envelope.headerOut = new Element[1]; envelope.headerOut[0] = buildAuthHeader(namespace, authheader, headers); } } /** * 执行请求 * * @param context * @param url * @param namespace * @param action * @param params * @param responseHandler */ public void send(Context context, String url, String namespace, String action, Map params, SoapResponseHandler responseHandler) { if (params != null) { final StringBuilder paramsStr = new StringBuilder(url); paramsStr.append('/') .append(action); if (params.size() > 0) { paramsStr.append('?'); } final SoapObject rpc = new SoapObject(namespace, action); for (final Map.Entry entry : params.entrySet()) { rpc.addProperty(entry.getKey(), entry.getValue()); paramsStr.append(entry.getKey()) .append('=') .append(entry.getValue()) .append('&'); } Log.d(TAG, "sendRequest " + paramsStr.toString()); envelope.bodyOut = rpc; } sendRequest(new HttpTransportSE(url, TIMEOUT), envelope, namespace, responseHandler, context); } /** * 构建auth header * * @param namespace * @param authheader * @param params * @return */ private Element buildAuthHeader(String namespace, String authheader, Map params) { final Element header = new Element().createElement(namespace, authheader); for (final Map.Entry entry : params.entrySet()) { final Element element = new Element().createElement(namespace, entry.getKey()); element.addChild(Node.TEXT, entry.getValue()); header.addChild(Node.ELEMENT, element); } return header; } /** * 取消请求 * * @param context * @param mayInterruptIfRunning */ public void cancelRequests(Context context, boolean mayInterruptIfRunning) { final List>> requestList = requestMap.get(context); if (requestList != null) { for (final WeakReference> requestRef : requestList) { final Future request = requestRef.get(); if (request != null) { request.cancel(mayInterruptIfRunning); } } } requestMap.remove(context); } /** * 发生请求 * * @param ht * @param envelope * @param namespace * @param responseHandler * @param context */ protected void sendRequest(HttpTransportSE ht, SoapSerializationEnvelope envelope, String namespace, SoapResponseHandler responseHandler, Context context) { final Future request = threadPool.submit(new SoapRequest(ht, envelope, namespace, responseHandler)); if (context != null) { // Add request to request map List>> requestList = requestMap.get(context); if (requestList == null) { requestList = new LinkedList<>(); requestMap.put(context, requestList); } requestList.add(new WeakReference>(request)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy