如何将PC上的本地tomcat服务器连接到真正的Android设备?

时间:2014-05-01 01:11:31

标签: java android tomcat servlets netbeans

我在将Android设备连接到pc的本地tomcat服务器时遇到了问题。以下是android代码..

public class Nxtstp extends Activity implements OnClickListener {

EditText txt;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nxtstp);
    btn =(Button) findViewById(R.id.Ent);
    btn.setOnClickListener(this);   
};      


class conn extends AsyncTask<Void, Void, Void>
{

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {
            public void run() {
             try{
                 URL url = new URL("http://10.0.2.2:8084/Wiremserv/servlet1");
                 URLConnection connection = url.openConnection();


                 //inputString = URLEncoder.encode(inputString, "UTF-8");
                 String msg="Verify";


                 connection.setDoOutput(true);
                 OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                 out.write(msg);
                 out.close();

                 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                 final String returnString="Verfied";
                 final String ret = in.readLine();
                 if(returnString==ret){
                     Context cont = getApplicationContext();
                     String ms="Connection verified";
                     int dur=Toast.LENGTH_SHORT;

                     Toast tst = Toast.makeText(cont, ms, dur);
                     tst.show();

                     Intent i = new Intent("com.lnct.wirem3.MAINSERV");
                     startActivity(i);    }

                 in.close();

                 }catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


                    }

                    }).start();
        return null;
    }

以下是netbeans上的servlet代码:

public class servlet1 extends HttpServlet {



 private static final long serialVersionUID = 1L;

public servlet1() {
    super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.getOutputStream().println("Hurray !! This Servlet Works");

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


     try {


        File song = new File("E:/Music/digit.mp3");
        URL url = song.toURI().toURL();
        Player player=Manager.createPlayer(url);
        try {
            int length = request.getContentLength();
            byte[] input = new byte[length];
                       ServletInputStream sin = request.getInputStream();
                       int c,count=0;
                      while ((c = sin.read(input, count, input.length-count)) != -1) {
            count +=c;
        }
        sin.close();
            String str = new String(input);
            response.setStatus(HttpServletResponse.SC_OK);
            OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());

                if("Verify".equals(str)){
                    String st="Verified";
                    writer.write(st);
                    writer.flush();
                    writer.close();
                }
        }




        catch (IOException e) {


            try{
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().print(e.getMessage());
                response.getWriter().close();
            } catch (IOException ioe) {
            }
        }
    } catch (NoPlayerException ex) {


        Logger.getLogger(servlet1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

现在我想要的是我想将我的应用程序连接到这个servlet,这样点击验证按钮时,我的新活动就会启动。 servlet只是比较字符串以向android设备发送验证消息。请告诉我如何让它在我的Android设备上运行(三星galaxy S推进GT-I9070)

1 个答案:

答案 0 :(得分:1)

在servlet中重写此方法:

protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
    throws ServletException, IOException 

在此处显示您的doGet代码,它可以正常使用。