客户端代码
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
NSDictionary *dict = @{@"method":@"setAddress",@"username":@"lisi",@"address":@"beijing"};
[manager POST:USERSERVLET_PATH parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *status = [responseObject objectForKey:@"status"];
if([@"success" isEqualToString:status]){
NSLog(@"设置Address成功");
//更新UI
_addressLabel.text = address;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"设置Address失败");
}];
服务器用的是java的servlet
PrintWriter out = null;
try {
out = response.getWriter();
String address = request.getParameter("address");
//要想通过此方法取得参数值,客户端要这样设置(默认就是此配置,可以不写)
//manager.requestSerializer = [AFHTTPRequestSerializer serializer];
String username = request.getParameter("username");
UserDao dao = new UserDaoImpl();
String status = dao.setAddress(username,address);
Map<String,String> map = new HashMap<String,String>();
map.put("status", status);
JSONObject result = JSONObject.fromMap(map);
out.print(result.toString());
} catch (IOException e) {
e.printStackTrace();
}finally{
out.flush();
out.close();
}