3

我正在尝试使用从 Android 应用程序本地创建的 Web 服务。我的问题是,在我的 Android 应用程序中,在某个时刻,我必须提供一个带有如下参数的 URL:http://localhost:8080/CalculatorApp/CalculatorWSService/add?i=1&j=1

CalculatorWS我使用的 Web 服务在哪里,add是其中的操作,i并且jadd操作参数。现在我正在使用一个示例应用程序计算器(来自 NetBeans)进行测试,我想检索正确的 URL 以提供给我的 Web 服务客户端(Android 应用程序),以便它可以返回一个 XML 来解析。

我尝试使用上面提到的那个 URL,但它不起作用。

有人知道正确的网址是什么吗?

4

4 回答 4

5

您需要将 URL 设置为 10.0.2.2: portNr

portNr = ASP.NET 开发服务器给定的端口我当前的服务在 localhost:3229/Service.svc 上运行

所以我的网址是 10.0.2.2:3229

我用这种方式解决了我的问题

我希望它有帮助...

于 2012-12-06T08:49:29.640 回答
4

使用此网址:

http://10.0.2.2:8080/CalculatorApp/CalculatorWSService/add?i=1&j=1

由于 Android 模拟器在虚拟机上运行,​​因此我们必须使用此 IP 地址而不是localhost or 127.0.0.1

于 2010-12-16T03:21:26.993 回答
1

如果您使用的是模拟器,请阅读以下段落摘自:从模拟环境中引用 localhost

如果您需要引用主机的 localhost,例如当您希望模拟器客户端联系运行在同一主机上的服务器时,请使用别名 10.0.2.2 来引用主机的环回接口。从模拟器的角度来看,localhost (127.0.0.1) 指的是它自己的环回接口。

于 2010-12-16T02:44:28.947 回答
0

就像你在评论中所说的那样,我将在此处粘贴一些代码来帮助你弄清楚如何处理,这段代码尝试连接到 Web 服务并解析检索到的 InputStream,就像 @Vikas Patidar 和 @MisterSquonk 说的那样,你必须像他们解释的那样在android代码中配置url。所以,我发布我的代码

以及调用 HttpUtils 的示例...

public static final String WS_BASE = "http://www.xxxxxx.com/dev/xxx/";
public static final String WS_STANDARD = WS_BASE + "webserviceoperations.php";
public static final String REQUEST_ENCODING = "iso-8859-1";

    /**
         * Send a request to the servers and retrieve InputStream
         * 
         * @throws AppException
         */
        public static Login logToServer(Login loginData) {
            Login result = new Login();
            try {
                // 1. Build XML
                byte[] xml = LoginDAO.generateXML(loginData);
                // 2. Connect to server and retrieve data
                InputStream is = HTTPUtils.readHTTPContents(WS_STANDARD, "POST", xml, REQUEST_ENCODING, null);
                // 3. Parse and get Bean
                result = LoginDAO.getFromXML(is, loginData);
            } catch (Exception e) {
                result.setStatus(new ConnectionStatus(GenericDAO.STATUS_ERROR, MessageConstants.MSG_ERROR_CONNECTION_UNKNOWN));

            }
            return result;
        }

和我的类 HTTPUtils 中的方法 readHTTPContents

/**
     * Get the InputStream contents for a specific URL request, with parameters.
     * Uses POST. PLEASE NOTE: You should NOT use this method in the main
     * thread.
     * 
     * @param url
     *            is the URL to query
     * @param parameters
     *            is a Vector with instances of String containing the parameters
     */
    public static InputStream readHTTPContents(String url, String requestMethod, byte[] bodyData, String bodyEncoding, Map<String, String> parameters)
            throws AppException {
        HttpURLConnection connection = null;
        InputStream is = null;
        try {
            URL urlObj = new URL(url);
            if (urlObj.getProtocol().toLowerCase().equals("https")) {
                trustAllHosts();
                HttpsURLConnection https = (HttpsURLConnection) urlObj
                        .openConnection();
                https.setHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
                connection = https;
            } else {
                connection = (HttpURLConnection) urlObj.openConnection();
            }
            // Allow input
            connection.setDoInput(true);
            // If there's data, prepare to send.
            if (bodyData != null) {
                connection.setDoOutput(true);
            }
            // Write additional parameters if any
            if (parameters != null) {
                Iterator<String> i = parameters.keySet().iterator();
                while (i.hasNext()) {
                    String key = i.next();
                    connection.addRequestProperty(key, parameters.get(key));
                }
            }
            // Sets request method
            connection.setRequestMethod(requestMethod);
            // Establish connection
            connection.connect();
            // Send data if any

            if (bodyData != null) {
                OutputStream os = connection.getOutputStream();
                os.write(bodyData);
            }
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new AppException("Error HTTP code " + connection.getResponseCode());
            }
            is = connection.getInputStream();
            int numBytes = is.available();
            if (numBytes <= 0) {
                closeInputStream(is);
                connection.disconnect();
                throw new AppException(MessageConstants.MSG_ERROR_CONNECTION_UNKNOWN);
            }

            ByteArrayOutputStream content = new ByteArrayOutputStream();

            // Read response into a buffered stream
            int readBytes = 0;
            while ((readBytes = is.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes);
            }
            ByteArrayInputStream byteStream = new ByteArrayInputStream(content.toByteArray());
            content.flush();
            return byteStream;
        } catch (Exception e) {
//          Logger.logDebug(e.getMessage());
            throw new AppException(e.getMessage());
        } finally {
            closeInputStream(is);
            closeHttpConnection(connection);
        }
    }

希望这可以帮助你...

于 2010-12-21T16:36:03.870 回答