我需要从 android 应用程序调用 Web 服务。我有在 java 中创建的 web 服务的 wsdl 文件,我没有相同的源代码。在 java 中,我使用“生成客户端”选项从 wsdl 生成文件,然后使用 Axis.jar 通过我的 java 应用程序调用 Web 服务。当我单击“生成客户端”选项时,会生成文件以及一些编译错误。但是当我在 java 应用程序中做同样的事情时,生成的文件没有任何编译错误。请告知android预期的正确方法。问候,
问问题
3053 次
1 回答
0
在 android 中,您可以使用 ksoap 库并调用所需的 web 服务。使用构建路径包含 ksoap 库并使用以下代码调用 Web 服务。在这个路径中找到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());
}
}
于 2012-06-02T04:11:10.310 回答