gpt4 book ai didi

android - 将 Android 网络服务连接到本地主机

转载 作者:行者123 更新时间:2023-11-29 00:24:24 24 4
gpt4 key购买 nike

我正在与这个特殊的事情作斗争,但无法解决它。请帮助我......我正在尝试将 android 程序连接到在 apache 服务器上运行的本地主机。

我在互联网上找到了直接连接到他们自己网站的例子。因此,他们将 namespace (在以下程序中)与他们的 namespace 一起提供。但是,由于我在本地主机上有我的 Web 应用程序,我应该提及什么?

(我在这里粘贴我的代码)主 Activity .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&#8221"; //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 的本地别名,它解析为自身。

目前在您的代码中,每台设备都将尝试访问其自身托管的服务。您必须将 localhost 替换为托管端点的服务器的 ip

关于android - 将 Android 网络服务连接到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20785448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com