HttpResponse response = client.execute(request);在avd浏览器中工作,但不在应用程序中

时间:2013-06-26 11:57:44

标签: android android-emulator

我使用以下代码使用get方法向我的笔记本电脑上的xampp服务器上运行的servlet发送一些信息,AVD也在同一台笔记本电脑上运行。我可以使用AVD中的浏览器访问servlet,但是可以从我创建的活动中访问。

public void get() throws Exception{
            new Thread() {
                @Override
                public void run() {
                    TextView tv=(TextView) findViewById(R.id.statusView);
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet();
                    try{
                    URI address=new URI("http://10.0.2.2:8080/HelloWorld?FirstName=saligrma&LastName=verma");
                    request.setURI(address);
                    tv.setText("URI build \n");
                    }
                    catch(Exception e)
                    {
                        //koi bat ni na;
                        tv.setText("Not able to build the URI \n");
                    }
                   // setProxyIfNecessay(context, request);
                    try {
                        HttpResponse response = client.execute(request);
                       // tv.setText("StatusCode: " + response.getStatusLine().getStatusCode() );
                    } catch (Exception e) {
                        tv.setText("some error occured ");
                    }
                }
            }.start();
        }

我的servlet代码在这里

public void doGet(HttpServletRequest request,
                    HttpServletResponse response) throws IOException

  {  java.sql.Connection con = null;

         String url = "jdbc:mysql://localhost:3306/myDataBase";
         String user="root";
         String password="root";
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String fn= request.getParameter("FirstName");
            String ln= request.getParameter("LastName");
        try {Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(url, user, password);
            Statement st = (Statement) con.createStatement();
                st.executeUpdate("INSERT INTO firstName VALUES('"+fn+"')");

            con.close();
            }

         catch (Exception ex) {
          out.println("<p>  Error Error Occured </p>");

           } 
  }

1 个答案:

答案 0 :(得分:0)

前一段时间发生在我身上。尝试使用计算机的IP(您可以使用linux / mac中的ifconfig和windows命令行中的ipconfig / all)而不是http://10.0.2.2:8080。从AVD中的应用程序访问localhost有点问题。