title: IOException: unexpected end of stream thrown HttpUrlConnection
body: <p>I am getting an <code>IOException: unexpected end of stream</code> when trying to read the <code>inputStream</code> of an <code>HttpUrlConnection</code>. I have tested with other URL's like <code>http://google.com</code> and added <code>urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0");</code> testing for an error on another helpful persons advice but no error occurred. My target SDK and buildSDK match. I am fetching results using the same class for other fragments with different URL's without problems. Since trying to change to another API I've come across this problem. </p>
  
  <p>The URL I'm trying to fetch JSON results from - <code>http://eventregistry.org/json/article?query=%7B%22%24query%22%3A%7B%22%24and%22%3A%5B%7B%22sourceUri%22%3A%7B%22%24and%22%3A%5B%22bbc.co.uk%22%5D%7D%7D%2C%7B%22lang%22%3A%22eng%22%7D%5D%7D%7D&amp;action=getArticles&amp;apikey=342f5f25-75ac-44b5-8da0441508e871e8&amp;resultType=articles&amp;articlesSortBy=date&amp;articlesCount=10&amp;articlesIncludeArticleImage=true</code></p>
  
  <p>My code to fetch the JSON -  </p>
  
  <pre><code>package com.example.android.greennewswire;
  
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.util.Log;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.util.ArrayList;
  
  public final class QueryUtils {
  
      private static final String LOG_TAG = QueryUtils.class.getSimpleName();
  
      private QueryUtils(){
      }
  
      public static ArrayList&lt;News&gt; fetchNewsData(String requestUrl) {
          URL url = createUrl(requestUrl);
          String jsonResponse = null;
          try {String testString = readUrl(requestUrl); Log.i(LOG_TAG, "testString: " + testString);
              jsonResponse = makeHttpRequest(url);
          } catch (IOException e) {
              Log.e(LOG_TAG, "Error making HTTP request", e);
          }
          if (jsonResponse == null) {
              return new ArrayList&lt;News&gt;();
          }
          ArrayList&lt;News&gt; news = extractBooks(jsonResponse);
  
          return news;
      }
  
      public static URL createUrl(String stringUrl){
          URL url = null;
          try {
              url = new URL(stringUrl);
          } catch (MalformedURLException e) {
              Log.e(LOG_TAG, "Malformed URL", e);
          } return url;
      }
  
      private static String makeHttpRequest(URL url) throws IOException {
          String jsonResponse = null;
  
          if (url == null) {
              return jsonResponse;
          }
  
          HttpURLConnection urlConnection = null;
          InputStream inputStream = null;
          InputStream errorStream = null;
  
          try {
              urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setReadTimeout(10000);
              urlConnection.setRequestMethod("GET");
              urlConnection.setConnectTimeout(10000);
              urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0");
              urlConnection.connect();
              Log.e(LOG_TAG, "Response code: " + urlConnection.getResponseCode());
  
  
              if (urlConnection.getResponseCode() == 200) {
                  inputStream = urlConnection.getInputStream();
                  jsonResponse = readFromInputStream(inputStream);
              } else {
                  Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
              }
          } catch (IOException e) {
              String string = e.getCause().toString();
              Log.e(LOG_TAG, "Problem reading from input stream, " + string, e);
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect();
              } if (inputStream != null) {
                  inputStream.close();
              }
          }
          return jsonResponse;
      }
  
  
      private static String readFromInputStream(InputStream inputStream) throws IOException {
          StringBuilder output = new StringBuilder();
          if (inputStream != null) {
              InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
              BufferedReader reader = new BufferedReader(inputStreamReader);
              String line = reader.readLine();
              while (line != null) {
                  output.append(line);
                  line = reader.readLine();
              }
          }
          return output.toString();
      }
  </code></pre>
  
  <p>Stack trace </p>
  
  <pre><code>Problem reading from input stream, java.io.EOFException: \n not found: size=14639 content=7b2261727469636c6573223a7b22726573756c7473223a5b7b226964223a2231...
                                                                             java.io.IOException: unexpected end of stream on Connection{eventregistry.org:80, proxy=DIRECT@ hostAddress=185.49.3.27 cipherSuite=none protocol=http/1.1} (recycle count=0)
                                                                                 at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)
                                                                                 at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
                                                                                 at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:905)
                                                                                 at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:789)
                                                                                 at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
                                                                                 at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
                                                                                 at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:501)
  </code></pre>