title: communicatin from android app to dynamic web servlet
body: <p>Hello i'm trying to get a simple app running on a emulator that will send data to a servlet running on the same PC using ( http:// 10.0.2.2:8080/...) as ip to connect to the host pc</p>
  
  <p>Additional reading: This is a test of princable that will hopefully allow me to run an app on a phone and connect to the servlet running at home witch will be used to ultimately connect to a mysql database.
  i have some code that seems to do what i want but i'm not sure if it's rite or if i'm implementing it correctly</p>
  
  <p>/** Android Side **/</p>
  
  <pre><code>import java.io.DataOutputStream; 
  import java.io.OutputStream; 
  import java.net.URL; 
  import java.net.URLConnection; 
  
  import android.app.Activity; 
  import android.os.Bundle; 
  import android.util.Log; 
  import android.view.View; 
  import android.view.View.OnClickListener; 
  import android.widget.Button; 
  import android.widget.EditText; 
  import android.widget.TextView;
  import android.widget.Toast;
  
  /**adapted code from online**/
  public class Test extends Activity { 
       static final String TAG = "Test"; 
      // Called when the activity is first created.  
  
      protected static EditText username; 
      protected static EditText password;
      TextView tview;
      String uname ="";
  
      @Override 
      public void onCreate(Bundle icicle) { 
          super.onCreate(icicle); 
          setContentView(R.layout.testlogin); 
  
          username = (EditText)findViewById(R.id.username); 
          password = (EditText)findViewById(R.id.password);
          tview = (TextView)findViewById(R.id.view);
          Toast.makeText(this, "started",1).show();
          tview.append("\nstarted\n"); 
      } 
  
      public void login(View v) { //in testlogin.xml//android:onClick="login"
          Toast.makeText(this, "button pressed",1).show();
          Log.v(TAG, "button pressed");
          //tview.append("clicked\n");
          uname=username.getText().toString(); 
          networkthread ob = new networkthread(uname); 
      } 
  }
  
  class networkthread implements Runnable { 
          private static final String TAG = "Test"; 
          String uname; 
          public networkthread(String uname) { 
                  this.uname =  uname; 
                  Thread t = new Thread(this); 
                  t.start(); 
          } 
          public void run(){
  
  
                  Log.v(TAG, "inside sub thread"); 
  
                  try { 
                      URL ob = new URL("http://10.0.2.2:8080/Server/test"); 
                      URLConnection           conn = ob.openConnection(); 
                      conn.setDoInput(true); 
                      conn.setDoOutput(true); 
                      OutputStream out = conn.getOutputStream(); 
                      DataOutputStream dos =  new DataOutputStream(out); 
                      dos.writeInt(uname.getBytes().length); 
                      dos.write(uname.getBytes(),0,uname.getBytes().length); 
                      dos.flush(); 
                      dos.close(); 
                      conn = null; 
                  }catch(Exception e){ 
                          Log.v(TAG, "Exception:" +e); 
                          e.printStackTrace(); 
                  } 
          } 
  } 
  </code></pre>
  
  <p>/** Servlet side in Dynamic web project **/</p>
  
  <pre><code>import java.io.*; 
  import java.net.*; 
  import javax.servlet.*; 
  import javax.servlet.http.*; 
  
  public class test extends HttpServlet { 
      private InputStream is = null; 
      private OutputStream os = null; 
      private DataInputStream dis = null; 
      private DataOutputStream dos = null; 
      boolean isValid = false; 
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
          try{ 
              is = request.getInputStream(); 
              dis = new DataInputStream(is); 
              int  len = dis.readInt(); 
              byte data[] = new byte[len]; 
              dis.read(data,0,len); 
              String userName = new String(data); 
              System.out.println("username:" +userName); 
              is.close(); dis.close(); 
          } catch(Exception e){ 
              System.out.println(e); 
          } 
      } 
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
          processRequest(request, response); 
      } 
  
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
          processRequest(request, response); 
      } 
  
  
      public String getServletInfo() { 
          return "Short description"; 
      } 
  
  }
  </code></pre>
  
  <p>I have some experience with both android and servlets
  there is no errors when i run this but the messages aren't being revived on the servlet side. i think i might be something to do with the app not waiting for a response from the servlet or the servlet not running at the same time the app requests a connection or something similar </p>
  
  <p>Is there an obvious reason for it not working and how can i fix this so i can move on to using the test app on a phone to connect to the servlet
  any help would be appreciated and i hope to be active on this post until i fix the issue</p>