Android之SOAP协议与WebService服务器交互,解决超时的问题


网络搜索大部分不能实际解决问题。特意将解决方法写下。
创建MyAndroidHttpTransport 类 ,

package com.example.omhandroid.lib;

import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.ServiceConnection;

import java.io.IOException;
import java.net.SocketException;

public class MyAndroidHttpTransport extends HttpTransportSE {

private int timeout = 600000; //默认超时时间为60s*10
private int readTimeout = 600000; //默认超时时间为60s*10
public MyAndroidHttpTransport(String url) {
super(url);
}

public MyAndroidHttpTransport(String url, int timeout) {
super(url);
this.timeout = timeout;
}
public ServiceConnection getServiceConnection() throws IOException {
return new org.ksoap2.transport.ServiceConnectionSE(this.proxy, this.url, this.timeout, this.readTimeout);
}
}



调用如下:
private static String SOAP_ADDRESS = ""; //http://195.100.15.33:8000/WebService1.asmx
// 命名空间
static String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
// 调用方法的名称
private static String OPERATION_NAME = ""; //HelloWorld1
 

// 生成调用WebService方法调用的soap信息,并且指定Soap版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// 是否调用DotNet开发的WebService
envelope.dotNet = true;

envelope.setOutputSoapObject(request);

MyAndroidHttpTransport httpTransport = new MyAndroidHttpTransport(SOAP_ADDRESS);

httpTransport.debug = true;
try {
httpTransport.call(WSDL_TARGET_NAMESPACE + OPERATION_NAME, envelope);
Object response = envelope.getResponse();
return response.toString();
} catch (Exception exception) {
exception.printStackTrace();
return "";
}

实测有效,可以放心使用
原文地址:https://www.cnblogs.com/ninilwh/p/11607429.html