2017-01-07 5 views
0

私はアンドロイドのネットワークを学んでいるので、ウェブサイトAPIからコンテンツを取得できます。私はUdacityのチュートリアルを見て、HttpURLConnectionを作るために必要なこれらのコード行を知る必要がありました。しかし、もし私がHttpsのURLからコンテンツを引き出す必要があればどうしますか? HttpsURL接続の場合、以下のコードでどのような変更が必要かをご提案ください。HttpsURLConnection in Android

の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()の独自の実装を作成すると、うまくいくでしょう。

関連する問題