title: Android Studio: Get a connection to a URL
body: <p>I am having a little bit of trouble getting JSON to load in my project.</p>
  
  <p>I used <a href="https://stackoverflow.com/questions/43079460/how-to-read-json-data-from-url-in-android">this</a>  getJSON method listed in the answers but it won't work. I figured out that simply the <code>connect()</code> function is throwing a <code>RuntimeException</code>.</p>
  
  <p>Here is my whole class including the getJSON method from another question:</p>
  
  <pre><code>private class GetUnis extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected Void doInBackground(Void... params) {
          HttpsURLConnection con = null;
          try {
              URL u = new URL(URL2);
              con = (HttpsURLConnection) u.openConnection();
  
              con.connect();
  
  
              BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
              StringBuilder sb = new StringBuilder();
              String line;
              while ((line = br.readLine()) != null) {
                  sb.append(line + "\n");
              }
              br.close();
  
              test = sb.toString();
  
  
          } catch (MalformedURLException ex) {
              ex.printStackTrace();
          } catch (IOException ex) {
              ex.printStackTrace();
          } finally {
              if (con != null) {
                  try {
                      con.disconnect();
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>And I am calling it like this: new GetUnis().execute();</p>
  
  <p>Edit: Here is the StackTrace</p>
  
  <pre><code>java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                     at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                     at java.lang.Thread.run(Thread.java:818)
                                                                                  Caused by: java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
                                                                                     at com.example.lukaskohl.myapplication.SelectUni$GetUnis.doInBackground(SelectUni.java:138)
                                                                                     at com.example.lukaskohl.myapplication.SelectUni$GetUnis.doInBackground(SelectUni.java:131)
                                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                     at java.lang.Thread.run(Thread.java:818) 
  </code></pre>
  
  <p> </p>