title: Android -urlConnection.getInputStream() fails - InputStream stays null
body: <p>I'm trying to retrieve JSON data from the Google Directions Server in Android. But getting the inputstream fails and an exception is thrown. I get the error message ,,null" and that the inputstream is null.</p>
  
  <p>What am I doing wrong with the HTTP connection? The URL is working on my browser.</p>
  
  <pre><code>     private String doRequest() {
      String response = null;
      HttpURLConnection urlConnection = null;
      InputStream in = null;
      URL url = null;
      String queryString ="https://maps.googleapis.com/maps/api/directions/json?origin=40.71926430971954,-74.26603317260742&amp;destination=40.74309523218185," +
              "-74.24732208251953&amp;sensor=false&amp;mode=walking&amp;alternatives=true&amp;key=AIzaSyASF2b0E0HHilJL5I936vqRbiBjM5XuPRA";
  
  
      try {
          url = new URL(queryString);
          urlConnection = (HttpURLConnection) url.openConnection();
          in = urlConnection.getInputStream();
      }
  
      catch (Exception e) {
          {
              StringBuilder builder = new StringBuilder();
              if (response == null)
                  builder.append("Response is null.");
              if (url == null)
                  builder.append("url is null.");
              if (urlConnection == null)
                  builder.append("UrlConnection is null.");
              if (in == null)
                  builder.append("Inputstream is null.");
  
              return builder.toString() +e.getMessage();
          }
  
  
      } finally {
          if (urlConnection != null)
              urlConnection.disconnect();
      }
      if (response == null)
      {
          return "sorry, response is null. At least it passed the exceptions.";
      }
      return response;
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p><a href="https://i.stack.imgur.com/uoguz.png" rel="nofollow noreferrer">Here is the error message</a></p>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <p>Okay, I've tried a mix of every ones suggestions. Now when I execute the code nothing happens. I can't open a dialog, so I don't know what problem it is this time.</p>
  
  <pre><code>public void getJSON(View v)
  {
     AsyncTask&lt;String, Void, String&gt;hello = new GetData(this).execute(queryString);
  }
  </code></pre>
  
  <p>import android.app.Dialog;</p>
  
  <p>import android.app.ProgressDialog;</p>
  
  <p>import android.content.Context;</p>
  
  <p>import android.content.DialogInterface;</p>
  
  <p>import android.net.http.HttpsConnection;</p>
  
  <p>import android.os.AsyncTask;</p>
  
  <p>import android.support.v7.app.AlertDialog;</p>
  
  <p>import android.view.View;</p>
  
  <p>import java.io.InputStream;</p>
  
  <p>import java.net.HttpURLConnection;</p>
  
  <p>import java.net.URL;</p>
  
  <p>import javax.net.ssl.HttpsURLConnection;</p>
  
  <p>(public class GetData extends AsyncTask)</p>
  
  <p>{</p>
  
  <pre><code>private Exception exception;
  public String response;
  private Context context;
  private ProgressDialog dialog = null;
  public GetData(Context context)
  {
      this.context = context;
  }
  
  protected String doInBackground(String... urls)
  {
      this.response = doRequest(urls[0]);
  
      return response;
  }
  
  protected void onPostExecute() {
      // TODO: check this.exception
      // TODO: do something with the feed
  
      if (this.exception != null &amp;&amp; response != null)
      {
          getDialog(getResponse(), context).show();
      }
      else
      {
          getDialog("sorry" + exception.getMessage(), context).show();
      }
  
  }
  
  private String doRequest(String queryString) {
      String response1 = null;
      HttpsURLConnection urlConnection = null;
      InputStream in = null;
      URL url = null;
  
      try {
          url = new URL(queryString);
          urlConnection =(HttpsURLConnection) url.openConnection();
  
          urlConnection.setRequestMethod("GET"); // or POST
          urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
          urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
          urlConnection.setDoInput(true);
          urlConnection.setDoOutput(true);
  
          int responseCode = urlConnection.getResponseCode();
  
          if (responseCode == HttpsURLConnection.HTTP_OK)
          {
              in = urlConnection.getInputStream();
              response1 = convertStreamToString(in);
          }
  
      } catch (Exception e) {
          {
              StringBuilder builder = new StringBuilder();
              if (response1 == null)
                  builder.append("Response is null.");
              if (url == null)
                  builder.append("url is null.");
              if (urlConnection == null)
                  builder.append("UrlConnection is null.");
              if (in == null)
                  builder.append("Inputstream is null.");
  
              return builder.toString() + e.getMessage();
          }
  
      } 
  
      finally
      {
          if (urlConnection != null)
              urlConnection.disconnect();
      }
  
      if (response1 == null)
      {
          return "sorry, response is null. At least it passed the exceptions.";
      }
      return response1;
  }
  
  private String convertStreamToString(java.io.InputStream is)
  {
      java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
      return s.hasNext() ? s.next() : "";
  }
  
  public String getResponse()
  {
      return this.response;
  }
  
  
  public Dialog getDialog(String string, Context context) {
      AlertDialog.Builder builder = new AlertDialog.Builder(context);
      builder.setMessage(string);
      builder.setPositiveButton("OK",
              new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                  }
              });
      return builder.create();
  
  }
  </code></pre>