How to connect to servlet from android app?
<p>I am trying to make an application which has connection with the servlet. The debugger doesn't show any error and neither the servlet has any error but still code is not working
This is the android application My_appActivity.java</p>

<pre><code>package com.m_app.first_app;

import java.io.DataInputStream; 
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.m_app.first_app.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class My_appActivity extends Activity{
    public static final String TAG = "SearchRecord";
    private EditText mEditText1;
    private Button mButton1;

    @Override
    public void onCreate(Bundle icicle) 
    {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(icicle);
        setContentView(R.layout.main);

     // Obtain handles to UI objects
        mEditText1 = (EditText) findViewById(R.id.editText1);
        mButton1 = (Button) findViewById(R.id.button1);


     // Register handler for UI elements
        mButton1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                Log.d(TAG, "mButton1 clicked");
                String keyword = mEditText1.getText().toString();
                networkthread ob = new networkthread(keyword);
            }           
        });    
    }
}

class networkthread implements Runnable
{
    public static final String TAG = "SearchRecord";
    String keyword;
    public networkthread(String keyword)
    {
        this.keyword=keyword;
        Thread t = new Thread(this);
        t.start();
    }
    public void run()
    {
        Log.v(TAG,"Inside the sub thread"); 
        try 
        {
            Log.v(TAG,"Inside try");
            Log.v(TAG,"Before conn");
            URL url = new URL("http://10.0.2.2:8080/My_project/yahoo");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();           
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Accept", "application/octet-stream");
            conn.connect();
            Log.v(TAG,"After conn");
            OutputStream out = conn.getOutputStream();
            Log.v(TAG,"Before DOS");
            DataOutputStream dos = new DataOutputStream(out);
            Log.v(TAG,"After DOS");
            dos.writeInt(keyword.getBytes().length);
            dos.write(keyword.getBytes(),0,keyword.getBytes().length);
            dos.flush();
            dos.close();
            InputStream is = conn.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            String status = dis.readLine();
            conn = null;
            Log.v(TAG,"Finish try");
        } 
        catch (Exception e)
        {
            Log.v(TAG,"Exception: "+e);
            e.printStackTrace();
        }   
    }  
}
</code></pre>

<p>The Servlet is :</p>

<pre><code>import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.*;
import java.net.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.*;
import java.io.*;

public class search extends HttpServlet {
    private InputStream is = null;
    private DataInputStream dis = null;
    PrintWriter out;
    protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException , IOException{
        try{
            out=response.getWriter();
            is = request.getInputStream();
            dis = new DataInputStream(is);
            int len = dis.readInt();
            byte data[] = new byte[len];
            dis.read(data,0,len);
            String Keyword = new String(data);
            out.print("Keyword :"+Keyword);
            is.close();
            dis.close();
            String s= "success";

        } catch(Exception e)
        {
            out.print(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        service(request,response);
    }       
}
</code></pre>
