【发布时间】:2014-01-14 03:29:53
【问题描述】:
我正在与这个特殊的事情作斗争,但无法解决它。请帮助我... 我正在尝试将 android 程序连接到在 apache 服务器上运行的本地主机。
我在互联网上找到了直接连接到他们自己网站的示例。因此,他们为名称空间(在以下程序中)提供了名称空间。 但是,由于我的 Web 应用程序位于本地主机中,我应该提什么?
(我在这里粘贴我的代码) MainActivity.java
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private String METHOD_NAME = "sum"; // our webservice method name
private String NAMESPACE = "http://calculator.backend.web.org”"; //I have to give the name space here I guess
private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private static final String URL = "http://localhost:8081/Test/services/Caluclate?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.txtAddition);
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("i", 5);
request.addProperty("j", 15);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION,envelope);
Object result = envelope.getResponse();
System.out.println(" Result : " + result.toString());
((TextView) findViewById (R.id.txtAddition)).setText("Addition : "+result.toString());
} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById (R.id.txtAddition)).setText("Error: " + E.getClass().getName() + ":" + E.getMessage());
}
}
}
Calculate.java(网络应用程序中的程序)
public class Caluclate {
public int sum() {
return (1);
}
public int subtract(int i, int j) {
return (i - j);
}
public int multiply(int i, int j) {
return (i * j);
}
public int divide(int i, int j) {
return (i / j);
}
}
谢谢你帮助我...
【问题讨论】:
-
localhost 替换为 127.0.0.1 或 10.0.2.2
标签: android web-services localhost