title: Trying to connect to servlet from Android Emulator, doesn't work?
body: <p>I'm trying to connect to a servlet in localhost from my Android Emulator.</p>
  
  <p>I created a project in Eclipse named SimpleHttpGetRequest with an activity named "HttpGetServletActivity".</p>
  
  <p>I created in NetBeans a project named "HttpGetRequest" containing a servlet.</p>
  
  <p>The code in my "HttpGetServletActivity" activity is :</p>
  
  <pre><code>package com.mobdev.simplehttprequest;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.net.URLConnection;
  import android.app.Activity;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class HttpGetServletActivity extends Activity implements OnClickListener {
  Button button;
  TextView outputText;
  
  public static final String URL = "http://10.0.2.2:8080/HttpGetRequest/HelloWorldServlet";
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      findViewsById();
  
      button.setOnClickListener(this);
  }
  
  private void findViewsById() {
      button = (Button) findViewById(R.id.button);
      outputText = (TextView) findViewById(R.id.outputTxt);
  }
  
  public void onClick(View view) {
      GetXMLTask task = new GetXMLTask();
      task.execute(new String[]{ URL });
  }
  
  private class GetXMLTask extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected String doInBackground(String... urls) {
          String output = null;
          for (String url : urls) {
              output = getOutputFromUrl(url);
          }
          return output;
      }
  
      private String getOutputFromUrl(String url) {
          StringBuffer output = new StringBuffer("");
          try {
              InputStream stream = getHttpConnection(url);
              BufferedReader buffer = new BufferedReader(
                      new InputStreamReader(stream));
              String s = "";
              while ((s = buffer.readLine()) != null)
                  output.append(s);
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          return output.toString();
      }
  
      // Makes HttpURLConnection and returns InputStream
      private InputStream getHttpConnection(String urlString)
              throws IOException {
          InputStream stream = null;
          URL url = new URL(urlString);
          URLConnection connection = url.openConnection();
  
          try {
              HttpURLConnection httpConnection = (HttpURLConnection) connection;
              httpConnection.setRequestMethod("get");
              httpConnection.connect();
  
              if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                  stream = httpConnection.getInputStream();
              }
          } catch (Exception ex) {
              ex.printStackTrace();
          }
          return stream;
      }
  
      @Override
      protected void onPostExecute(String output) {
          outputText.setText(output);
      }
  }
  }
  </code></pre>
  
  <p>The source code of my servlet is :</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class HelloWorldServlet extends HttpServlet {
  
  public HelloWorldServlet() {
      super();
  }
  
      @Override
  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      out.println("Hello Android !!!!");
  }
  }
  </code></pre>
  
  <ul>
  <li>I deployed my servlet in Apache server (i'm using xampp);</li>
  <li>I added permission for network connection</li>
  </ul>
  
  <p>When I run my App and click on the button, the App crashes, and I don't know why !!
  Can anybody help me, please ? Im' stuck.</p>
  
  <p>I tried :</p>
  
  <ul>
  <li>Wifi connection : I did run my App on a real device, instead of "10.0.2.2" I put the ip adress of     my PC but it doesn't work ;</li>
  <li>Access to project HttpGetrequest from Android Emulator browser, it worked ;</li>
  </ul>