2014-10-28 33 views
0

我有一個Android應用程序,它將模型名稱和String發送給java servlet,然後將它添加到servlet網頁的組合框中。這不是現在發生的權利,但以下情況例外在logcat中看到Android和servlet通信和顯示

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); 

    } 
} 

回答

0

網址URL =新的URL( 「HTTP:8080/MyServletProject/Connection_Servlet」); 在我給出錯誤路徑的URL中有錯誤。 正確的URL是 URL url = new URL(「http:8080/ScannerAutomationWebApplication/Connection_Servlet」);