【问题标题】:android invoke service with ksoupandroid 使用 ksoup 调用服务
【发布时间】:2012-11-05 03:31:03
【问题描述】:

我创建了一个asp.net服务,代码如下:

namespace ArttechApps
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string getBoughtApps(String imei)
        {
            return "this is the string to return";// JAVA : SOAP 
        }
    }
}

我需要一个调用此服务的 android 代码。 这是java代码: 公共静态无效连接(字符串网址) {

        webServices ws=new webServices("getBoughtApps", "http://tempuri.org/", "http://localhost:12409/getApps.asmx");

        ws.AddProperty("imei", "000000000000000", String.class);

        String res="";
        try 
        {

            SoapObject respuesta = ws.CallWebService();

            res=respuesta.toString();

            Log.d("Response", res);
        } catch (Exception e) 
        {

            e.printStackTrace();
        }
        result=res;
    }

这是安卓的代码。 [已编辑] 有人可以帮我做吗?

【问题讨论】:

标签: android asp.net soap ksoap


【解决方案1】:

这里有我用来使用 KSOAP2 for android 使用 Web 服务的类

import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;

public class webServices {

    public static String METHOD_NAME;
    public static String NAMESPACE;
    public static String URL;   
    public SoapSerializationEnvelope Envelope_class = null; 
    private SoapObject request;

    public webServices(String NombreMetodo, String Namespace, String URLWService )
    {       
        METHOD_NAME = NombreMetodo;
        NAMESPACE= Namespace;
        URL= URLWService;   
        request= GetSoapObject(METHOD_NAME);
    }

    public void AddProperty(String Name, Object Value ,Type tipo)
    {
        PropertyInfo prop = new PropertyInfo();     
        prop.setName(Name);
        prop.setValue(Value);
        prop.setType(tipo);
        request.addProperty(prop);  
    }

    private SoapObject GetSoapObject (String Methodname)
    {
        return new SoapObject(NAMESPACE,METHOD_NAME);
    }

    private static SoapSerializationEnvelope GetEnvelope(SoapObject Soap)
    {
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
       envelope.dotNet = true;     
       envelope.setOutputSoapObject(Soap);
       return envelope;

     }

    public SoapObject CallWebService() throws IOException, XmlPullParserException 
    {       
        SoapObject response=null;
        SoapSerializationEnvelope Envelope = GetEnvelope(request);
        Envelope.bodyOut=request;
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL);
        androidHttpTransport.debug=true;
        try
           { 

               androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope);              
               response = (SoapObject) Envelope.getResponse();      
               Envelope_class = Envelope;
           }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.d("AndroidRequest",androidHttpTransport.requestDump);
            Log.d("AndroidResponse",androidHttpTransport.responseDump);
            return null;
        }   

        return response;
    }

    public Object CallWebServicePrimitive() throws SoapFault
    {
        SoapObject request = GetSoapObject(METHOD_NAME);    
        SoapSerializationEnvelope Envelope = GetEnvelope(request);

         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
         try {
            androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope);
        } catch (IOException e) {
            Log.d("ErrorApp", e.getMessage().toString());
        } catch (XmlPullParserException e) {
            Log.d("ErrorApp", e.getMessage().toString());
        }   

        SoapPrimitive response= (SoapPrimitive) Envelope.getResponse();
        return response;
    }

}

如果您使用的是 WCF 服务,则需要更改:

SoapSerializationEnvelope(SoapEnvelope.VER11) 到: SoapSerializationEnvelope(SoapEnvelope.VER12)

并将 URL 指向您的 SVC URL

【讨论】:

  • 我有这段代码,但它在 androidHttpTransport.call(NAMESPACE + METHOD_NAME, Envelope); 上抛出异常;例外是 SocketTimeoutException
  • 尝试增加超时并确保您的 Web 服务 URL 正确。尝试通过浏览器访问该服务,telnet 以检查其是否正在运行
  • 请粘贴您用于 NAMESPACE、METHOD_NAME、URL 等的值
  • 方法名:getBoughtApps 命名空间:tempuri.org url:localhost:12409/getApps.asmx
  • 尝试 namespace = "tempuri.org" 最后加上斜线。你的 ws amsx 确定吗?对我来说似乎是 wcf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多