Android 开发笔记“调用.net webservice遇到的问题”

1、An exception occurred: org.ksoap2.SoapFault

SoapFault - faultcode: ‘soap:Server‘ faultstring: ‘服务器无法处理请求。 ---> 未能加载文件或程序集“MySql.Data, Version=6.3.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d”或它的某一个依赖项。系统找不到指定的文件。‘ faultactor: ‘null‘ detail: org.kxml2.kdom.Node@423841d0

原因:webservice 中采用了强类型数据集(DataSet)连接MySql数据库,简化数据库底层开发

解决:装一下mysql-connector-net-6.3.9.msi就可以了,文章下面有地址

2、Android访问webservice 帮助类,源码如下:

package com.sinobpo.nbsmt.webservice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class WebServiceUtil {

    /**
     * .Net WebService调用方法
     * 
     * @param nameSpace
     * @param methodName
     * @param webServiceUrl
     * @param map
     * @return
     */
    public static String doCallWebService(String nameSpace, String methodName,
            String webServiceUrl, Map map) {
        String xmlStr = null;
        SoapObject _soapObject = new SoapObject(nameSpace, methodName);

        // 如果有参数,设置调用方法参数
        Set _keySet = map.keySet();// 返回键的集合
        Iterator _iterator = _keySet.iterator();

        while (_iterator.hasNext()) // 第一种迭代方式取键值
        {
            Object key = _iterator.next();
            _soapObject.addProperty(key.toString(), map.get(key));
        }
        try {
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            // 是否可以调用.netWebService
            envelope.dotNet = true;
            envelope.setOutputSoapObject(_soapObject);

            HttpTransportSE ht = new HttpTransportSE(webServiceUrl, 5000);

            ht.call(nameSpace + methodName, envelope);

            if (envelope.getResponse() != null) {
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                xmlStr = response.toString();
            }

        } catch (Exception e) {
            System.out.println("prop: " + e.toString());
            e.printStackTrace();
            return null;
        }
        return xmlStr;
    }

    /**
     * Java WebService调用方法
     * 
     * @param nameSpace
     * @param methodName
     * @param webServiceUrl
     * @param map
     * @return
     */
    public static String doCallWebServiceForJava(String nameSpace,
            String methodName, String webServiceUrl, Map map) {
        String xmlStr = null;
        // 创建httpTransportSE传输对象
        HttpTransportSE ht = new HttpTransportSE(webServiceUrl);
        ht.debug = true;
        // 使用soap1.1协议创建Envelop对象
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // 实例化SoapObject对象
        SoapObject request = new SoapObject(nameSpace, methodName);
        /**
         * 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
         * */
        // 如果有参数,设置调用方法参数
        Set _keySet = map.keySet();// 返回键的集合
        Iterator _iterator = _keySet.iterator();

        while (_iterator.hasNext()) // 第一种迭代方式取键值
        {
            Object key = _iterator.next();
            request.addProperty(key.toString(), map.get(key));
        }
        // request.addProperty("json", "YYYY");
        // soapheader在这里
        // Element[] header = new Element[1];
        // Element lv = new Element().createElement(nameSpace, "json");
        // lv.addChild(Node.TEXT, "");
        // 将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
        envelope.bodyOut = request;
        // envelope.dotNet = false;
        try {
            // 调用webService
            ht.call(null, envelope);
            if (envelope.getResponse() != null) {
                // SoapObject result = (SoapObject) envelope.bodyIn;
                // String name = envelope.bodyIn.toString();
                System.out.println("envelope.getResponse()="
                        + envelope.getResponse().toString());
                xmlStr = "";
            } else {
                xmlStr = null;
            }
        } catch (Exception e) {
            System.out.println("prop: " + e.toString());
            e.printStackTrace();
        }

        return xmlStr;
    }

    public static String doCallWebServiceForPhp(String webServiceUrl) {
        String xmlStr = null;
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);
        HttpParams p = new BasicHttpParams();
        p.setParameter("user", "1");
        HttpClient httpclient = new DefaultHttpClient(p);
        HttpPost httppost = new HttpPost(webServiceUrl);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", "1"));
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            xmlStr = httpclient.execute(httppost, responseHandler);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return xmlStr;
    }
}

 

3、invalid project description

 技术分享

a、此错误简单翻译为:无效的项目描述。点击错误对话框中的detail按钮,提示为:overlaps the location of another project的描述,翻译成中文也就是说导入的项目位置与另外一个项目重叠。

b、导致此错误的很有可能的原因之一为:此项目在eclipse的工作目录之下。所以,将项目拷贝到另一个目录下,然后再尝试导入,大部分情况下是可以解决此问题的。

 

 

 

资料地址

mysql-connector-net-6.3.9.msi:http://yunpan.cn/cKx77DgN6Z9EX (提取码:80c7)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。