简体   繁体   English

从android应用程序调用Web服务

[英]Calling Web Services from android application

I need to invoke a web service from android app. 我需要从android应用程序调用网络服务。 I have the wsdl file of a web service created in java, i don't have the source code of the same. 我有用Java创建的Web服务的wsdl文件,我没有相同的源代码。 In java, i use the "Generate Client" option to generate the files from a wsdl and then invoke the web service via my java app using Axis.jar. 在Java中,我使用“ Generate Client”选项从wsdl生成文件,然后使用Axis.jar通过我的Java应用程序调用Web服务。 When i click the "Generate Client" option the files are generated along with some compilation errors. 当我单击“生成客户端”选项时,将生成文件以及一些编译错误。 But the same thing when i am doing in an java app, the generated files does not have any compilation errors. 但是当我在Java应用程序中执行操作时,同样的事情,生成的文件没有任何编译错误。 Please inform the proper way of doing the same from the android prospective. 请从android前瞻性工具中告知执行此操作的正确方法。 Regads, Regads,

In android you can use ksoap library and call the required webservice. 在android中,您可以使用kso​​ap库并调用所需的网络服务。 Include ksoap library using build path and use the following code to invoke the webservice. 使用构建路径包括ksoap库,并使用以下代码来调用Web服务。 find the ksoap library in this path Ksoap library path 在此路径中找到ksoap库Ksoap库路径

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;

public class SVCWebServiceActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/GetByteArray";
    private static final String METHOD_NAME = "GetByteArray";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://IPAddress/Serviceforc/Service.asmx";
    private String byteStr = "srikar";
    private byte[] bytedata = byteStr.getBytes();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("bytedata is "+bytedata[0]);
        HttpTransportSE httpTransport = null;
        MarshalBase64 marshal = null;
        SoapObject request = null;
        request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo pi = new PropertyInfo();       
        pi.setName("value");
        pi.setValue(bytedata);
        pi.setType(MarshalBase64.BYTE_ARRAY_CLASS);
        request.addProperty(pi);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        marshal = new MarshalBase64();
        marshal.register(envelope);
        envelope.setOutputSoapObject(request);

        httpTransport = new HttpTransportSE(URL);

        Object response;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        } catch (Exception exception) {
            exception.printStackTrace();
            System.out.println("Exception is " + exception.toString());
            // strRes = exception.toString();
            response = exception.toString();

        }
        System.out.println("Response is "+response.toString());
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM