title: Android connection to localhost
body: <p>I'm trying to connect my android application to a local host url thanks to wamp server but it doesn't work. My goal here, is to fetch json data and parse these data. For my test, i'm using a device not the emulator and i use permission in AndroidManifest.xml : </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p>My url looks like this :</p>
  
  <pre><code>String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
  </code></pre>
  
  <p>i tried :</p>
  
  <pre><code>http://localhost/
  http://10.0.2.2:8080/
  http://10.0.2.2/
  </code></pre>
  
  <p>But it never worked so far :</p>
  
  <pre><code>    java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
  
      failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
  
      java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
  </code></pre>
  
  <p>Then i tried with a json url test found on the internet : <a href="http://headers.jsontest.com/" rel="noreferrer">http://headers.jsontest.com/</a></p>
  
  <p>It worked really good and i got json data at this address. So i guess my code is good and the issue here is my localhost url, i don't know what should be its exact form..
  I read many threads about it but i didn't find a solution.</p>
  
  <p>Here my code :</p>
  
  <p>Main activity :</p>
  
  <pre><code>public class MainActivity extends Activity {
      private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
  
      private ListView lv = null;
      private Button bGetData;
  
      @Override
      public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final JsonDownloaderTask task = new JsonDownloaderTask(this);
          lv = (ListView) findViewById(R.id.list);
  
          bGetData = (Button)findViewById(R.id.getdata);
          bGetData.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {               
                  task.execute(url);                      
              }
          });
      }
  
      public void jsonTaskComplete(JSONArray data){
          //todo
      }   
  }
  </code></pre>
  
  <p>AsyncTask :</p>
  
  <pre><code>public class JsonDownloaderTask extends AsyncTask&lt;String, String, JSONArray&gt; {
  
      MainActivity ma;
  
      public JsonDownloaderTask(MainActivity main){
          ma = main;
      }
  
      @Override
      protected JSONArray doInBackground(String... url) {
          JSONParser jParser = new JSONParser();
          // Getting JSON from URL
          JSONArray jsonArray = null;
          try {
              jsonArray = jParser.getJSONFromUrl(url[0]);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return jsonArray;        
      }
  
      protected void onPostExecute(JSONArray data){
  
          ma.jsonTaskComplete(data);
      }
  }
  </code></pre>
  
  <p>JSONParser :</p>
  
  <pre><code>public class JSONParser {
      String data = "";
      JSONArray jsonArray = null;        
      InputStream is = null;
  
      public JSONParser(){}
  
      // Method to download json data from url
      public JSONArray getJSONFromUrl(String strUrl) throws IOException{
          try{
              URL url = new URL(strUrl);
  
              // Creating an http connection to communicate with url
              HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  
              // Connecting to url
              urlConnection.connect();
  
              // Reading data from url
              is = urlConnection.getInputStream();
  
              BufferedReader br = new BufferedReader(new InputStreamReader(is));
  
              StringBuffer sb  = new StringBuffer();
  
              String line = "";
              while( ( line = br.readLine())  != null){
                  sb.append(line);
              }
              is.close();
              data = sb.toString();
  
              //br.close();
  
              jsonArray = new JSONArray(data);
  
          }catch(Exception e){
              Log.d("Exception while downloading url", e.toString());
          }finally{
              is.close();
          }
  
          return jsonArray;
      }
  }
  </code></pre>