Java 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");
}
}