我在 Android 中调用 asmx Web 服务,但每次运行我的应用程序时都会出现错误。
HTTP request faild.HTTP Status Code : 400
我将 - 更改为 <10.0.2.2:port/WebService1.asmx>
我也试过
但它给出了同样的错误。
服务在 .net 中完美运行,但需要 android 的解决方案。请帮助。谢谢你。
代码:
网络服务:
[WebMethod]
public string HelloWorld()
{
return "Hello World - This is nikki";
}
MainActivity.java
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:1553/WebService1.asmx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call();
}
public void call()
{
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("passonString", "Rajapandian");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = (Object)envelope.getResponse();
Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}