2017-01-07 80 views
0

我正在學習android中的網絡,以便我可以從網站API獲取內容。我正在閱讀Udacity教程,並瞭解了製作HttpURLConnection所需的這些代碼行。但是如果我必須從Https網址中提取內容呢?請在下面的代碼中建議對HttpsURL連接進行哪些修改。Android中的HttpsURLConnection

HttpURLConnection類:

private String makeHttpRequest(URL url) throws IOException { 
      String jsonResponse = ""; 
      HttpURLConnection urlConnection = null; 
      InputStream inputStream = null; 
      try { 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 
       urlConnection.setReadTimeout(10000 /* milliseconds */); 
       urlConnection.setConnectTimeout(15000 /* milliseconds */); 
       urlConnection.connect(); 
       inputStream = urlConnection.getInputStream(); 
       jsonResponse = readFromStream(inputStream); 
      } catch (IOException e) { 
       // TODO: Handle the exception 
      } finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
       if (inputStream != null) { 
        // function must handle java.io.IOException here 
        inputStream.close(); 
       } 
      } 
      return jsonResponse; 
+0

這個鏈接可以幫助你http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically – rogerwar

回答

0
readFromStream(inputStream); 

這行代碼是負責讀取內容。在你發佈的代碼示例中,它似乎是URL正在提取的JSON內容。 創建你自己的readFromStream()的實現,你會很開心。