要在Android 10中进行WebService调用,可以使用以下解决方法:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class WebServiceCaller {
public static String callWebService(String url, String soapAction, String xmlInput) {
try {
URL requestUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("SOAPAction", soapAction);
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(xmlInput.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
return response.toString();
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
使用示例:
String url = "http://example.com/myservice";
String soapAction = "http://example.com/myservice/action";
String xmlInput = "" +
" " +
"" +
"" +
"value1 " +
"value2 " +
" " +
" " +
" ";
String response = WebServiceCaller.callWebService(url, soapAction, xmlInput);
添加Ksoap2库的依赖:
implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'
使用示例:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebServiceCaller {
public static String callWebService(String url, String namespace, String methodName, PropertyInfo[] properties) {
try {
SoapObject request = new SoapObject(namespace, methodName);
if (properties != null) {
for (PropertyInfo property : properties) {
request.addProperty(property);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(url);
transport.call(namespace + methodName, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
使用示例:
String url = "http://example.com/myservice";
String namespace = "http://example.com/webservice";
String methodName = "MyWebServiceMethod";
PropertyInfo parameter1 = new PropertyInfo();
parameter1.setName("Parameter1");
parameter1.setValue("value1");
parameter1.setType(String.class);
PropertyInfo parameter2 = new PropertyInfo();
parameter2.setName("Parameter2");
parameter2.setValue("value2");
parameter2.setType(String.class);
PropertyInfo[] properties = new PropertyInfo[]{parameter1, parameter2};
String response = WebServiceCaller.callWebService(url, namespace, methodName, properties);
这些示例均为基本示例,实际情况中可能需要根据WebService的具体要求进行参数设置和解析返回结果。