0

我正在尝试从我的 Android 应用程序调用网络服务。我部署了一个 Web 服务,它使用 Java 中的 Hibernate 插入一条记录(产品条形码、产品名称),我编写了一个活动来测试它。

当我在我的 AVD 上运行它时,我得到了一个 connectionException :Connection refused

这是我的代码:

package com.market_helper.app;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.serialization.PropertyInfo;

public class Market_helper_androidActivity extends Activity {

    private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/Main";
    private static final String METHOD_NAME = "insertProduct";
    private static final String NAMESPACE = "http://localhost:7675/market_helper/services/";
    private static final String URL = "http://localhost:7675/market_helper/services/Main?wsdl";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
           request.addProperty("barCode", "12345");

           request.addProperty("productName", "abc");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = envelope.getResponse();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我在 Android 上的第一个网络服务,我不确定代码是否正确。我正在调用一个名为insertProduct(String barCode,String productName). Web 服务正在运行并经过测试。

4

6 回答 6

1
private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/Main"; 

你给了 LocalHost

尝试使用 ip 地址而不是 LocalHost

喜欢这个

   private static final String SOAP_ACTION = "http://10.120.159.87:7675/market_helper/services/Main"; 
于 2011-11-30T04:14:54.183 回答
0

在你的 AndroidManifest.xml 中确保你设置了 Internet 权限,一旦我设置了这个,我就遇到了同样的问题,一切都很好。

<uses-permission android:name="android.permission.INTERNET" />
于 2011-11-30T04:18:10.047 回答
0
private static final String URL = "http://localhost:7675/market_helper/services/Main?wsdl";

在这一行中,将“?wsdl”放在 URL 的末尾是错误的,将其删除并将您的 web 服务的扩展名(.asmx 或 .svc)放在末尾而不是它。像...

private static final String URL = "http://localhost:7675/market_helper/services/Main.asmx";
于 2011-11-30T04:24:35.950 回答
0

感谢您帮助我解决了
我使用 ipconfig 命令获取本地 ip的 url 的问题
我的 apache tomcat 服务器使用了端口 7675
我将该端口添加到我的防火墙异常端口以确保它可以正常工作。

并且没有我更改的 asmx 文件 ?wsdl
现在我可以从浏览器中看到 wsdl 描述。

我的命名空间来自 xml 文件“main.wsdl”
<schema elementFormDefault="qualified" targetNamespace="http://app.market_helper.com" xmlns="http://www.w3.org/2001/XMLSchema">

,正确的代码在这里: private static final String SOAP_ACTION = "http://app.market_helper.com/insertProduct"; private static final String METHOD_NAME = "insertProduct"; private static final String NAMESPACE = "http://app.market_helper.com"; private static final String URL = "http://192.168.1.2:7675/market_helper/services/Main?wsdl";
现在我可以从我的 Android 应用程序中使用 Web 服务。
我将我的正确代码发回给与我有同样问题的人。

于 2011-11-30T23:16:05.000 回答
0

我打开资源管理器时可以看到。我使用这个网站

“http://本地主机:49674/Service.svc”

我应该写什么?我总是在尝试捕获时出错。我在 .net webservice 中使用了 WCF。和实体框架。

     private final String NAMESPACE="http://tempuri.org/IService/";
     private final String SOAPACTION="http://tempuri.org/IService/GetTreatmentValues";
     private final String METHODNAME="GetTreatmentValues";    
    private final String URL="http:// local host:49674/Service.svc";

我的错误在哪里?(我也使用 ksoap 2.4)

于 2013-11-25T21:14:54.030 回答
-1
private static final String SOAP_ACTION = "http://localhost:7675/market_helper/services/insertProduct"; 

SOAP ACTION=NAMESPACE+METHOD NAME;
于 2011-11-30T04:30:10.670 回答