3030import java .io .BufferedReader ;
3131import java .io .File ;
3232import java .io .IOException ;
33- import java .io .InputStream ;
3433import java .io .InputStreamReader ;
3534import java .io .PrintWriter ;
35+ import java .net .URL ;
36+ import java .security .KeyManagementException ;
37+ import java .security .NoSuchAlgorithmException ;
3638import java .util .ArrayList ;
3739import java .util .Collections ;
3840import java .util .Iterator ;
3941import java .util .LinkedList ;
4042import java .util .List ;
4143
42- import org .apache .http .HttpEntity ;
43- import org .apache .http .HttpResponse ;
44- import org .apache .http .StatusLine ;
45- import org .apache .http .client .HttpClient ;
46- import org .apache .http .client .methods .HttpGet ;
47- import org .apache .http .impl .client .DefaultHttpClient ;
4844import org .json .JSONArray ;
4945import org .json .JSONException ;
5046import org .json .JSONObject ;
5854import android .net .NetworkInfo ;
5955import android .net .Uri ;
6056import android .os .AsyncTask ;
61- import android .os .Build ;
6257import android .os .Bundle ;
6358import android .util .Log ;
6459import android .widget .Toast ;
60+
61+ import javax .net .ssl .HttpsURLConnection ;
62+
6563import se .bitcraze .crazyflie .lib .bootloader .Bootloader ;
6664import se .bitcraze .crazyflie .lib .bootloader .FirmwareRelease ;
6765
@@ -227,11 +225,9 @@ private void downloadFile (String url, String fileName, String tagName) {
227225 DownloadManager .Request request = new DownloadManager .Request (Uri .parse (url ));
228226 request .setDescription ("Some description" );
229227 request .setTitle (fileName );
230- // in order for this if to run, you must use the android 3.2 to compile your app
231- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) {
232- request .allowScanningByMediaScanner ();
233- request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_HIDDEN );
234- }
228+ // in order for this if to run, you must use android 3.2 (API 11) to compile your app
229+ request .allowScanningByMediaScanner ();
230+ request .setNotificationVisibility (DownloadManager .Request .VISIBILITY_HIDDEN );
235231 request .setDestinationInExternalFilesDir (mContext , null , BootloaderActivity .BOOTLOADER_DIR + "/" + tagName + "/" + fileName );
236232
237233 // get download service and enqueue file
@@ -247,31 +243,38 @@ private void downloadFile (String url, String fileName, String tagName) {
247243 private String downloadUrl (String myUrl ) throws IOException {
248244 BufferedReader reader = null ;
249245 StringBuilder builder = new StringBuilder ();
250- HttpClient client = new DefaultHttpClient ();
251- HttpGet httpGet = new HttpGet (myUrl );
246+ URL url = new URL (myUrl );
247+
248+ // Retrofitting support for TLSv1.2, because GitHub only supports TLSv1.2
252249 try {
253- HttpResponse response = client .execute (httpGet );
254- StatusLine statusLine = response .getStatusLine ();
255- int statusCode = statusLine .getStatusCode ();
250+ HttpsURLConnection .setDefaultSSLSocketFactory (new TLSSocketFactory ());
251+ } catch (KeyManagementException e ) {
252+ e .printStackTrace ();
253+ } catch (NoSuchAlgorithmException e ) {
254+ e .printStackTrace ();
255+ }
256+
257+ HttpsURLConnection urlConnection = (HttpsURLConnection ) url .openConnection ();
256258
257- if (statusCode == 200 ) {
258- HttpEntity entity = response .getEntity ();
259- InputStream content = entity .getContent ();
260- reader = new BufferedReader (new InputStreamReader (content , "UTF-8" ));
259+ try {
260+ String responseMsg = urlConnection .getResponseMessage ();
261+ int responseCode = urlConnection .getResponseCode ();
261262
263+ if (responseCode == 200 ) {
264+ reader = new BufferedReader (new InputStreamReader (urlConnection .getInputStream (), "UTF-8" ));
262265 String line ;
263266 while ((line = reader .readLine ()) != null ) {
264267 builder .append (line );
265268 }
266269 } else {
267- Log .d (LOG_TAG , "The response is: " + response );
268- return "The response is: " + response ;
270+ Log .d (LOG_TAG , "The response is: " + responseMsg );
271+ return "The response is: " + responseMsg ;
269272 }
270273 } finally {
271- // Makes sure that the InputStream is closed after the app is finished using it.
272274 if (reader != null ) {
273275 reader .close ();
274276 }
277+ urlConnection .disconnect ();
275278 }
276279 return builder .toString ();
277280 }
0 commit comments