title: Problem connecting to servlet From Android client
body: <p>I am trying to connect to the Servlet (Tomcat localhost). Here is my code.</p>
  
  <p><code>ServletTest.java</code> (the client)</p>
  
  <pre><code>public class ServletTest extends Activity {
      private static final String TAG = "Test";
          /** Called when the activity is first created. */
  
      protected static EditText username;
      protected static EditText password;
  
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.main);
  
           username = (EditText)findViewById(R.id.username);
  
  
           Button goButton = (Button)findViewById(R.id.connect);
           goButton.setOnClickListener(mGoListener);
      }
  
      private OnClickListener mGoListener = new OnClickListener()
      {
          @Override
          public void onClick(View v)
          {
              //finish();
                  String uname=username.getText().toString();
                  Log.v(TAG, uname);
                  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(){
              try{
                  Log.v(TAG,"Inside try");
                  Log.v(TAG,"before con");
                  URL url=new URL("http://192.168.1.117/servletAndroid/TestAndroid");
                  HttpURLConnection con;
                  con=(HttpURLConnection)url.openConnection();
                  Log.v(TAG,"after con");
                  con.setDoInput(true);
                  con.setDoOutput(true);
                  con.setRequestMethod("POST");
                  con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  con.setRequestProperty("Accept", "application/octet-stream");
                  Log.v(TAG,"Before dos");
                  DataOutputStream dos=new DataOutputStream(con.getOutputStream());
                  Log.v(TAG,"After dos");
                  dos.writeUTF(uname.trim());
                  dos.flush();
                  dos.close();
                  Log.v(TAG,"Finish try");
              }
              catch(Exception e){
                  Log.v(TAG,"Exception"+e);
              }
  
          }
  
  }
  </code></pre>
  
  <p><code>TestServlet.java</code> (the server)</p>
  
  <pre><code>public class TestServlet extends HttpServlet {
      public void init() {
      }
  
      public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          try{
          System.out.println("test");
          DataInputStream in = new DataInputStream((InputStream) request.getInputStream());
          String uname = in.readUTF();
          //String password = in.readUTF();
          System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
  
          System.out.println(":::::::::::" + uname);
  
  
          response.setContentType("text/plain");
          //response.setContentLength(info.length());
          PrintWriter out = response.getWriter();
          out.println(uname);
  
          in.close();
          out.close();
          out.flush();
          }
          catch(Exception e){
              System.out.println("Exceptin "+ e);
          }
  
      }
  
      public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          doPost(request, response);
      }
  </code></pre>
  
  <p>But I didn't got anything in Tomcat console printed by the servlet. How is this caused and how can I solve it?</p>