Using a web service in an Android application on a local host

I am trying to use a web service that I created locally from an Android application. My problem is that in my Android application at some point I have to specify a URL with parameters that look like this: http: // localhost: 8080 / CalculatorApp / CalculatorWSService / add? I = 1 & j = 1

where the CalculatorWSweb service addis used is the operation in it, and ithey jare the parameters of the operation add. At the moment I am using an approximate application calculator (from NetBeans) for testing, and I want to get the correct URL to give my client a web service (Android application) so that he can return me the XML for analysis.

I tried using the above URL, but it does not work.

Does anyone know which correct URL to put?

+3
source share
4 answers

you need to set the url as 10.0.2.2: portNr

portNr = this port using ASP.NET Development Server my current service is running local: 3229 / Service.svc

so my url is 10.0.2.2:3229

I fixed my problem this way

Hope this helps ...

+5
source

Use this URL:

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

Since the Android emulator runs in a virtual machine, so we should use this IP address instead localhost or 127.0.0.1

+4

, , : localhost

, , , , , 10.0.2.2 . localhost (127.0.0.1) loopback.

+1

sharktiger, , , , , - InputStream, @Vikas Patidar @MisterSquonk , URL- Android, . ,

and an example of calling 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;
        }

and readHTTPContents method from my HTTPUtils class

/**
     * 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 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);
        }
    }

Hope this helps you ...

0
source

Source: https://habr.com/ru/post/1780584/


All Articles