2010-12-16 78 views
3

我正在嘗試使用從Android應用程序本地創建的Web服務。 我的問題是,在我的Android應用程序,在某一點上,我不得不放棄與參數的URL看起來像這樣:http://localhost:8080/CalculatorApp/CalculatorWSService/add?i=1&j=1在本地主機上的Android應用程序中使用Web服務

其中CalculatorWS是Web服務我使用,add是它和i操作和jadd操作的參數。現在我正在使用示例應用程序計算器(來自NetBeans)進行測試,並且我想要檢索正確的URL以將其提供給我的Web服務客戶端(Android應用程序),以便它可以讓我返回一個XML進行分析。

我試圖使用上面提到的URL,但它不起作用。

有人知道什麼是正確的URL嗎?

+0

請問您服務本地主機接口上聽? – 9000 2010-12-16 02:25:08

+0

我認爲問題並不在於該Android應用程序消耗,如果在NetBeans的Web應用程序不正確的輸出到輸入流(也許寫一個XML文件)的URL,並且在Android應用連接我沒有看到問題,也許我不明白你的問題 – Franco 2010-12-16 02:25:20

+0

你的服務在localhost接口上監聽嗎?在你的Android設備上安裝一個終端(如果你還沒有的話),說'netstat -lnt',看看有沒有列出127.0.0.1。 – 9000 2010-12-16 02:30:40

回答

1

如果您使用的是仿真器然後在下面的段落讀摘自:Referring to localhost from the emulated environment

如果您需要參考您的主機的本地主機,當 希望仿真器客戶端連接服務器的運行,如在相同的 主機上,使用別名10.0.2.2來引用主機的環回 接口。從仿真器的角度來看,localhost(127.0.0.1) 引用了它自己的回送接口。

4

使用此網址:

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

由於對虛擬機的Android模擬器中運行,因此我們有像您說的意見,使用該IP地址而不是localhost or 127.0.0.1

+0

一些代碼,我想先運行此: http://10.0.2.2:8080/但它總是拋出一個調用異常:( – sharktiger 2010-12-17 01:26:08

+0

@sharktiger:如果您發表評論或回覆使用(@)符號,其次是用戶名,否則它是沒有意義的。如果你有任何異常,那麼在這裏發佈你的錯誤日誌。 – 2011-01-12 06:27:44

0

sharktiger,我在這裏粘貼一些代碼來幫助你瞭解如何處理,這段代碼嘗試連接到一個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

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

希望這有助於你的方法readHTTPContents ...

5

您需要將URL設置爲10.0.2.2:portNr

portNr =由ASP.NET開發服務器的指定端口 我目前的服務正在運行 本地主機:3229/Service.svc

所以我的網址是10.0.2.2:3229

我會固定我這樣的問題

我希望它可以幫助...

相關問題