title: How to read json data from URL in Android
body: <p>I am having some issues with the following code. I am trying to read from the following Reddit URL</p>
  
  <pre><code>https://www.reddit.com/r/earthporn/.json?after=
  </code></pre>
  
  <p>Here is the code that is not correctly being executed. </p>
  
  <pre><code>public static String readContents(String url){
          try{
          InputStream input= new URL(url).openStream();
          Reader reader = new InputStreamReader(input);
          BufferedReader in = new BufferedReader(reader);
          String line, str;
          str = "";
          while ((line=in.readLine()) != null) {
              str += line;
              System.out.println(line);
          }
          return str;
          }catch(IOException e){
              Log.d("READ FAILED", e.toString());
              return null;
          }
      }
  }
  </code></pre>
  
  <p>And here is where I call it from</p>
  
  <pre><code>List&lt;Post&gt; fetchPosts(){
      String raw=RemoteData.readContents(url);
      List&lt;Post&gt; list=new ArrayList&lt;Post&gt;();
      try{
          JSONObject data=new JSONObject(raw).getJSONObject("data");
          JSONArray children=data.getJSONArray("children");
  
          //Using this property we can fetch the next set of
          //posts from the same subreddit
          after=data.getString("after");
  
          for(int i=0;i&lt;children.length();i++){
              JSONObject cur=children.getJSONObject(i)
                      .getJSONObject("data");
              Post p=new Post();
              p.title=cur.optString("title");
              p.url=cur.optString("url");
              p.numComments=cur.optInt("num_comments");
              p.points=cur.optInt("score");
              p.author=cur.optString("author");
              p.subreddit=cur.optString("subreddit");
              p.permalink=cur.optString("permalink");
              p.domain=cur.optString("domain");
              p.id=cur.optString("id");
              p.thumbnail=cur.optString("thumbnail");
              if(p.title!=null)
                  list.add(p);
          }
      }catch(Exception e){
          Log.e("fetchPosts()",e.toString());
      }
      return list;
  }
  </code></pre>
  
  <p>When debugging I get the following results:
  Raw is left empty.
  <a href="https://i.stack.imgur.com/Dx4DZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dx4DZ.png" alt="enter image description here"></a></p>
  
  <p>Does anyone have any clue as to why this it is not reading anything? I hope I included enough code for it to make sense. If anymore is needed, please let me know. </p>