title: Using HttpURLConnection on UI thread (Inside Activity)
body: <p>using this method for getting Latitude and longitute of location entered by user 
  working good in java but when I use this method in android , app stops automatically and exceptions/errors appears are shown in attached image  </p>
  
  <pre><code>public static String[] getLatLongPositions(String address) throws Exception
  {
      int responseCode = 0;
      String api = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + URLEncoder.encode(address, "UTF-8") + "&amp;sensor=true";
      URL url = new URL(api);
      HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
      httpConnection.connect();
      responseCode = httpConnection.getResponseCode();
  
      if(responseCode == 200)
      {
          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();;
          Document document = builder.parse(httpConnection.getInputStream());
          XPathFactory xPathfactory = XPathFactory.newInstance();
          XPath xpath = xPathfactory.newXPath();
          XPathExpression expr = xpath.compile("/GeocodeResponse/status");
          String status = (String)expr.evaluate(document, XPathConstants.STRING);
          if(status.equals("OK"))
          {
              expr = xpath.compile("//geometry/location/lat");
              String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
              expr = xpath.compile("//geometry/location/lng");
              String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
              return new String[] {latitude, longitude};
          }
          else
          {
              throw new Exception("Error from the API - response status: "+status);
          }
      }
      return null;
  }
  </code></pre>
  
  <p>Error:</p>
  
  <p><a href="https://i.stack.imgur.com/BoBo1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BoBo1.png" alt="error"></a></p>