我试图将一个 int 值传递给 Web 服务并期待回复。但我只在网络服务上获得价值。我的意思是它不接受我的输入值。安卓代码如下:
package com.example.fp1_webservicedropdown;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
public class MainActivity extends Activity {
TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.textView2);
final String NAMESPACE = "http://sample.com/";
final String METHOD_NAME = "SayHello";
final String SOAP_ACTION = "http://sample.com/SayHello";
final String URL = "http://localip/HellowWorld/Service1.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("SayHello", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
.getResponse();
result.setText("The web service returned " + resultString);
System.out.println(resultString);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Web服务代码如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace HellowWorld
{
[WebService(Namespace = "http://sample.com/")]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int SayHello(int a)
{
int t = 8 + a;
return t;
}
}
}
它只是返回当我通过 android 运行时,Web 服务返回 8。Insted 给予 Web 服务返回 40。非常感谢任何帮助。