title: Android - JSON Connection to Web Server / MySQL
body: <p>I am in my first android programming days, and I am stuck at this.</p>
  
  <p>I'm trying to establish a connection (log in page) with a MySQL database through PHP in an android project. I've been successful in the emulator with the host's ip address inside my network. However, I can't seem to connect to the external IP, which is fulcral because I mean to implement it on real devices.</p>
  
  <p>On the device, since it's outside the network, I can't connect either way.</p>
  
  <p>Result of unsuccessful connection is crash, and LogCat stays stuck at Button - Login.</p>
  
  <p>I've added Internet Permissions to the Manifest.</p>
  
  <p>The only change between a successful connection and an unsuccessful one is the adress (internal to external).</p>
  
  <pre><code>//JSON parser code:
  
  public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) {
  
      // Making HTTP request
      try {
  
          // defaultHttpClient
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpPost httpPost = new HttpPost(url);
          httpPost.setEntity(new UrlEncodedFormEntity(params));
  
          HttpResponse httpResponse = httpClient.execute(httpPost);
          HttpEntity httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
          Log.e("JSON", json);
      } catch (Exception e) {
          Log.e("Buffer Error", "Error converting result " + e.toString());
      }
  
      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);            
      } catch (JSONException e) {
          Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
  
      // return JSON String
  
      return jObj;
  
  }
  
  
  //Creating the JSON
  
  JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
  </code></pre>