我正在尝试从 android 使用 JAVA Web 服务。
这是我到目前为止所尝试的:
private void CallWebServiceDummy() {
// TODO Auto-generated method stub
try {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
soapEnvelope.dotNet = false;
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
StringArraySerializer a = new StringArraySerializer();
a.add("hello"); a.add("world"); String n0 = NAMESPACE;
pi = new PropertyInfo(); pi.setName("a"); pi.setValue(a);
pi.setType(a.getClass()); pi.setNamespace(n0);
Request.addProperty(pi);
String b = "my name"; pi = new PropertyInfo(); pi.setName("b");
pi.setValue(b); Request.addProperty(pi);
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
Log.d("test", "request: " + androidHttpTransport.requestDump);
Log.d("test", "response: " + androidHttpTransport.responseDump);
SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;
String c = resultsRequestSOAP.toString();
} catch (Exception e) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, e.getMessage(), duration);
toast.show();
toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
}
}
我的 Java 网络服务代码:
package MyPackage;
public class WebServiceClass {
public String addnumbers(String[] a, String b) {
String c = new StringBuilder("This the String1 ").append(a[0]).append(" merged with String2 ").append(b).toString();
return c;
}
}
我的全局:
private static final String NAMESPACE = "http://MyPackage"; private
static final String URL =
"http://10.0.2.2:8080/WebService/services/WebServiceClass?wsdl"; private
static final String SOAP_ACTION = "urn:addnumbers"; private static final
String METHOD_NAME = "addnumbers";
问题:
我收到的回复是:
addnumbersResponse{return=This the String1 merged with String2 my name; }
第一个参数未发送到 Web 服务。我试图删除这一行:
soapEnvelope.dotNet = 假;但它仍然无法正常工作。
伙计们请帮帮我。我被困了两天。感谢您提供的任何帮助。