【问题标题】:Android and servlet communication and displayingAndroid与servlet通信与显示
【发布时间】:2014-12-23 14:57:14
【问题描述】:

我有一个 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);

    }
}

【问题讨论】:

    标签: java android servlets data-stream


    【解决方案1】:

    URL url = new URL("http:8080/MyServletProject/Connection_Servlet"); URL 中有错误,我给出了错误的路径。 正确的网址是 URL url = new URL("http:8080/ScannerAutomationWebApplication/Connection_Servlet");

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多