關於ios開發使用AFNetWorking框架請求伺服器端取不到值的問題
阿新 • • 發佈:2019-02-19
客戶端程式碼
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();
}