Android使用KSoap2包访问WebService

2015-01-30 21:36:50|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:Android, Java, 移动应用|来源:唯设编程网

四. 使用

下面,我们实现一个通用的功能类KSoap2Utility,下面是这个类的实现代码:

package utility;

import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.ygh.vcsosapplication.R;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * KSoar2工具类,实现WebService的访问
 * Created by ygh on 2015/1/27.
 */
public class KSoap2Utility {
    private static HeaderProperty mSessionHeader = null;
    /***
     * 请求WebService
     * @param URL
     * @param METHOD_NAME
     * @param values
     * @param handler
     */
    public static void doTransaction(final String URL, final String METHOD_NAME, final Map<String, Object> values, final  Resources r, final Handler handler)
    {
        final String TAG ="Vik";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String NAMESPACE = r.getString(R.string.web_service_namespace);
                String SOAP_ACTION = NAMESPACE + METHOD_NAME;
                try {
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    Iterator iter = values.entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        request.addProperty(entry.getKey().toString(), entry.getValue());
                        Log.i(TAG, entry.getKey().toString() + ">>" + entry.getValue());
                    }

                    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    soapEnvelope.bodyOut = request;
                    soapEnvelope.dotNet = true;
                    soapEnvelope.setOutputSoapObject(request);

                    //处理cookie,保存session
                    // Build request header
                    LinkedList<HeaderProperty> reqHeaders = new LinkedList<HeaderProperty>();

                    if (mSessionHeader != null) {
                        reqHeaders.add(new HeaderProperty("Cookie", mSessionHeader.getValue())); //using the session id from last header
                    }


                    HttpTransportSE transport = new HttpTransportSE(r.getString(R.string.host)+URL,3000);
                    List<HeaderProperty> respHeaders = transport.call(SOAP_ACTION, soapEnvelope,reqHeaders);

                    // See note after code
                    for (HeaderProperty hp : respHeaders) {
                        if (hp != null && hp.getKey() != null
                                && hp.getKey().equalsIgnoreCase("set-cookie"))
                        {
                            mSessionHeader = hp;
                            break;
                        }
                    }

                    Object resultString = soapEnvelope.getResponse();
                    Log.i(TAG, "Result: " + resultString.toString());

                    //处理消息
                    Message message = handler.obtainMessage();
                    message.obj = resultString;
                    handler.sendMessage(message);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    Log.e(TAG, "Error: " + ex.getMessage());
                }
            }
        }).start();
    }
}
发表评论0条 】
网友评论(共?条评论)..
Android使用KSoap2包访问WebService