2015-08-15 121 views
1

我對使用查詢字符串發送HTTP帖子感到懷疑。在URL中使用查詢字符串發送HTTP帖子

我有下面的代碼如下,但代碼不工作。我嘗試通過網絡服務發送用戶名和密碼,嵌入在URL中,但它不工作。此代碼無法連接到Web服務。

@Override 
     protected String doInBackground(String... params) { 

      String result = ""; 

      try { 
        URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user="+user+"&senha="+password); 
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
        httpURLConnection.setRequestMethod("POST");    


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 
        String inputLine; 
        StringBuilder response = new StringBuilder(); 

        while ((inputLine = bufferedReader.readLine()) != null) { 
         response.append(inputLine); 
        } 

        result = response.toString(); 
        bufferedReader.close(); 
       } catch (Exception e) { 
        Log.d("InputStream", e.getMessage()); 
        } 

      return result; 
     } 
+1

在'GET'請求中使用URL參數。在'POST'request中,值應該在請求體中。 – Jens

+0

什麼是錯誤信息? – Jens

+0

檢查客戶端和服務器之間的連接。 192.168.0.11是本地網絡地址... –

回答

0

我想你的意思GET請求不是POST, ,你應該在編碼的查詢參數,「用戶」,並在你的情況下,「密碼」的變量。

URL url = new URL("http://192.168.0.11:8080/api/Usuario/doLogin?user=" + URLEncoder.encode(user, "UTF-8")+"&senha="+ URLEncoder.encode(password, "UTF-8"));