1

我有一个如下所示的登录 servlet:

@WebServlet(name="AndroidResponse", urlPatterns={"/androidres.do"})
public class AndroidResponse extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        String un,pw;
        un=request.getParameter("username");
        pw=request.getParameter("password");
        if(un.equalsIgnoreCase("test") && pw.equals("test"))
            out.print(1);
        else
            out.print(0);
    }
}

我试图在这样的主要活动中访问它:

response = CustomHttpClient.executeHttpPost("http://androidres.do/AndroidResponse", postParameters);
                        String res=response.toString();

我收到一条错误消息,提示主机不存在。获得响应的正确方法是什么?

4

1 回答 1

1

/androidres.do is not the host part of the servlet URL, but the path that comes after the host. The correct URL for calling your servlet should be http://YOUR_SERVLET_HOST/androidres.do.

So you need to know how to call the webserver that runs the servlet from the android device, for example:

response = CustomHttpClient.executeHttpPost("http://192.168.0.100:8080/androidres.do", postParameters);
String res=response.toString();

if the webserver can be reached on IP address 192.168.0.100 and port 8080.

于 2013-05-11T08:08:17.773 回答