如何在PC上将本地tomcat服务器连接到真实的android设备



我在连接我的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,以便在单击verify按钮时启动我的新活动。servlet只是比较字符串以向android设备发送验证消息。请告诉我如何让它在我的安卓设备上工作(三星galaxy S advance GT-I9070)

在servlet中重写此方法:

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

把你的doGet代码带到这里,它会工作得很好。

相关内容

  • 没有找到相关文章

最新更新