Android和servlet的通信和显示

时间:2014-10-28 09:04:57

标签: java android servlets data-stream

我有一个Android应用程序,它将模型名称和String发送到java servlet,然后将其添加到servlets网页的组合框中。在Logcat

中看到异常后,现在不会发生这种情况
10-28 14:25:05.733: D/Exception(7518): java.io.FileNotFoundException: MyServletProject/Connection_Servlet

这是我的应用代码

   public void onClick(View v) {

            switch (v.getId()){
            case R.id.doubleme:
        new Thread(new Runnable() {
            public void run() {

                try{
                    URL url = new URL("http://192.168.1.5:8080/MyServletProject/Connection_Servlet");
                    //URL url = new URL("http://10.0.2.2:8080/MyServletProject/DoubleMeServlet");
                    URLConnection connection = url.openConnection();

                    String inputString = "11";
                    String model=getDeviceName();
                    //inputString = URLEncoder.encode(inputString, "UTF-8");

                    Log.d("inputString", inputString);
                    Log.d("inputString",model);

                    connection.setDoOutput(true);
                   /* OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                    out.write(inputString);
                    out.close();*/
                    DataOutputStream out1 = new DataOutputStream(connection.getOutputStream());
                    out1.writeUTF(model);
                    out1.writeUTF(inputString);
                    out1.flush();
                    out1.close();

                    InputStream is =connection.getInputStream();
                    DataInputStream dis =new DataInputStream(is); 
                while(dis.available()==0);
                    String WTF = dis.readUTF();
                   dis.close();
                   Log.d("Message",WTF);

                    }catch(Exception e)
                    {
                        Log.d("Exception",e.toString());
                    }

            }

这是我的servlet代码

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setIntHeader("Refresh", 5);  

     request.setAttribute("item",list);
     request.getServletContext().getRequestDispatcher("/BarcodeScannerTest.jsp").forward(request, response);

        }
        boolean dataon = false;
        boolean Device_connected =false;
       public String model;
       ArrayList<String> list = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));


        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            InputStream is =request.getInputStream();
            DataInputStream dis =new DataInputStream(is); 
        while(dis.available()==0);
            model = dis.readUTF();
           String input = dis.readUTF();
           dis.close();    
            System.out.println("Model:"+model);
            list.add(model);
            OutputStream dout=response.getOutputStream();
            DataOutputStream dat = new DataOutputStream(dout);
            dat.writeUTF("Working");
            dat.flush();
            dat.close();
            doGet(request,response);

    }
}

1 个答案:

答案 0 :(得分:0)

网址url =新网址(&#34; http:8080 / MyServletProject / Connection_Servlet&#34;); 我给错了路径的URL出错了。 正确的网址是 URL url =新网址(&#34; http:8080 / ScannerAutomationWebApplication / Connection_Servlet&#34;);