1
我使用java android實現客戶端服務器代碼。問題是我無法連接到servlet。我的問題在哪裏?這裏是我的代碼:發送Post方法從Android客戶端到Java Servlet的數據
的Android代碼: ....
HttpClient client=new DefaultHttpClient();
HttpPost getMethod=new HttpPost("http://" + Server + "/RestaurantServer/Login");
try {
// Add your data
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Mode", "Login"));
nameValuePairs.add(new BasicNameValuePair("userName",txtusername.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password",txtpassword.getText().toString()));
getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
client.execute(getMethod);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
和服務器端:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String mode=request.getParameter("Mode");
if ("Login".equals(mode)) {
String userName= request.getParameter("userName");
String password= request.getParameter("password");
System.out.println("post method: "+userName + "! pass :"+password);
user = new User(password,userName);
if (UserDao.authenticate(user)==true)
out.write("Accept".toString());
else
out.write("Wrong".toString());
} else if ("Register".equals(mode)) {
String userName= request.getParameter("userName");
String password= request.getParameter("password");
String name= request.getParameter("name");
String email= request.getParameter("email");
String address= request.getParameter("address");
String phoneNumber= request.getParameter("phoneNumber");
System.out.println("reg mod: "+userName + "! pass :"+password);
user = new User(userName,password,name,email,address, phoneNumber);
if (UserDao.addUser(user) == true)
out.write("Added");
else
out.write("NotAdded");
}
} catch (Exception ex) {
System.out.println("Problem in message reading");
}
}
在開始時:ⅰ寫:@WebServlet(名稱= 「登錄」,URL模式= { 「/登錄」})我在doPost方法調用的processRequest:@覆蓋 保護無效的doPost(HttpServletRequest的請求,響應HttpServletResponse的) throws ServletException,IOException processRequest(request,response); } – user698494 2013-05-06 08:28:48
那麼你的mapping.post可能有問題你的web.xml – 2013-05-06 09:41:30