title: Parsing Json from URL in Android Studio
body: <p>I am trying to parse some JSON. I have lately been attempting to use <strong>GSON</strong> to do so in android studio. However, I can't seem to obtain a connection or do anything with the data. The data I would be looking at, if I could ever obtain it from the site would look something like this: </p>
  
  <pre><code>{"item":{"icon":"address_here",
  id=820","id":820,"type":"Default",
  "typeIcon":"catagory","name":"Name of Object",
  "description":"This is an annoying issue to get stuck on"}}
  </code></pre>
  
  <p>I have been using async, but to no avail. Perhaps a new way to look at this would be good. </p>
  
  <p>Here's my current code:</p>
  
  <pre><code>    public class JavaParser extends AsyncTask&lt;String, String, String&gt; {
  public String getInternetData() throws Exception {
      String sURL = "someurl.com"; //just a string
  
      // Connect to the URL using java's native library
  
      try {
          URL url = new URL(sURL);
          HttpURLConnection request = (HttpURLConnection) url.openConnection();
          Log.d("Debug:", "Made partial connection");
          try {
              request.connect();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          Log.d("Debug:", "Made connection");
      }
      catch(Exception e){
  
      }
      return "FAILURE";
  }
  private String response = null;
  
  @Override
  protected String doInBackground(String...params) {
      try {
          response = new JavaParser().getInternetData();
      }
      catch(Exception e){
      Log.d("Error",e.getMessage()
      );
      }
      return response;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In my main class:</p>
  
  <pre><code>    String response = new JavaParser().execute("URL").get();        //eventually I hope to pass the url here to be more dynamic
          //Convert to a JSON object to print data
          JsonParser jp = new JsonParser(); //from gson
          JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
          JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
  
          Gson gson = new Gson();
          Wrapper response = gson.fromJson(rootobj, Wrapper.class);
  
  
          Log.d("Out: ",rootobj.get("item\name").getAsString()); //just grab the name info
  </code></pre>
  
  <p>And my wrapper looks like so:</p>
  
  <pre><code>    public class Wrapper   {
  
  @SerializedName("name")
  public String item_name;
  
  @SerializedName("icon")
  public String iconURL;
  
  @SerializedName("description")
  public  String item_description;
  
  //more to be added if it works
  </code></pre>
  
  <p>}</p>
  
  <p>The program only ever makes partial connection. I cannot seem to get the line
          request.connect();
  to function whatsoever.</p>