我在让iPhone应用程序使用我的API正确发送POST时遇到问题。进行调用时,根本不返回任何结果。
-
POST 调用旨在发送消息(存储消息,检查某些参数,并启动"推送"通知)
-
发布调用适用于.php文件上的表单,但在通过应用程序使用时则不然。返回结果:{"消息":{"消息":{"已保存":1}},"推送":1,"错误":[]} 推送期间的错误是预期的,因为 test2.php 不会将消息发送到移动设备,而只是将消息发送到数据库。
-
这是应用程序的开机自检代码
NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *time = [dateFormatter stringFromDate:now]; NSLog(@"time : %@", time); NSLog(@"message : %@", self.messageTxt.text); NSString *urlString = [[Utils getSendMessageUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; [request setValue:[UserSession sharedInstance].customerTableNum forHTTPHeaderField:@"point"]; [request setValue:[UserSession sharedInstance].locationId forHTTPHeaderField:@"location"]; [request setValue:time forHTTPHeaderField:@"sent"]; [request setValue:self.messageTxt.text forHTTPHeaderField:@"message"]; [request setValue:[UserSession sharedInstance].phoneNumber forHTTPHeaderField:@"number"]; self.receivedData = [NSMutableData data]; NSURLConnection *sendConnection = [NSURLConnection connectionWithRequest:request delegate:self]; [sendConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [sendConnection start];
什么可能导致应用加载索引.php但没有结果?post变量设置正确,即使"push"失败,消息仍应输入数据库(就像test2.php成功一样)。
提前感谢您的任何见解。
-----------编辑----------------
以下是我的应用开发人员的回复:
pragma mark - NSURLConnection delegate
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse");
[self.receivedData setLength:0];
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData : %@", data);
[self.receivedData appendData:data];
}
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *resultString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(@"result : %@", resultS tring);
[self performSelectorOnMainThread:@selector(didSendMessage:) withObject:resultString waitUntilDone:NO];
}
I get the following log.
2012-04-11 00:13:59.177 M[7682:707] didReceiveResponse
2012-04-11 00:13:59.179 M[7682:707] result :
No header response is sent back.
该请求需要满足以下条件:
- 位置(数量)
- 点(数字) 数字
- (数字)
- 消息(字符串)
- 已发送(日期时间:年-月-日 。
这是一个没有安全部分的PHP代码来减少长度。
处理从 case = post 开始的所有内容的调用(get 用于检索消息)
$request = RestUtils::processRequest(); switch ($request->getMethod()){ case 'get': case 'post': $db = get_db(); $response = array(); $response["message"] = array(); $data = $request->getRequestVars(); $location = htmlspecialchars($data["location"]); $point = htmlspecialchars($data["point"]); $area = get_area($point, $location, $db); $employee = get_employee($request, $area, $db); $message_id = add_message($response["message"], $request, $area, $db); if($employee == false){ $response["error"] = "mo."; $response["pushed"] = 0; RestUtils::sendResponse(410, json_encode($response), 'application/json'); exit; } $response["pushed"] = array(); $response["error"] = array(); $number = htmlspecialchars($data['number']); switch($employee["phone_Type"]){ case 1: if(is_blocked($number)){ $response["error"] = "You're Number is Blocked"; $response["pushed"] = 0; RestUtils::sendResponse(503, json_encode($response), 'application/json'); exit; } if(push_android($employee["device_ID"], $message_id, $point, $location) == false){ $response["error"] = "Service Temporally Unavailable"; $response["pushed"] = 0; RestUtils::sendResponse(400, json_encode($response), 'application/json'); exit; } $response["pushed"] = 1; break; case 2: if(is_blocked($number)){ $response["error"] = "You're Number is Blocked"; $response["pushed"] = 0; RestUtils::sendResponse(503, json_encode($response), 'application/json'); exit; } push_iOS($employee["device_ID"], $message_id, $point, $location); $response["pushed"] = 1; break; default: $response["pushed"] = 0; $response["error"] = 'Unsupported Phone Type'; RestUtils::sendResponse(406, json_encode($response), 'application/json'); } $db->close(); RestUtils::sendResponse(200, json_encode($response), 'application/json'); break; }
以下是支持功能:
function build_post_query($area, $point, $location, $message, $number, $sent){ return "INSERT INTO messages (message_ID, area_ID, point_ID, location_ID, message_Sent, message_Text, message_Number, message_Viewed) VALUES (NULL, ".$area.", ".$point.", ".$location.", '".$sent."', '".$message."', '".$number."', 0)"; } function add_message(&$response, $request, $area, $db){ $data = $request->getRequestVars(); $point = htmlspecialchars($data["point"]); $location = htmlspecialchars($data["location"]); $area = get_area($point, $location, $db); $sent = htmlspecialchars($data["sent"]);// 'yyyy-mm-dd HH:mm:ss' format from users phone $message = htmlspecialchars($data["message"]); $number = htmlspecialchars($data["number"]); $query = build_post_query($area, $point, $location, $message, $number, $sent); $result = $db->query($query); $response["message"] = array('saved'=>$db->affected_rows); $message_id = $db->insert_id; return $message_id; } function get_area($p, $loc, $db){ $query = "SELECT area_ID FROM points WHERE point_ID = ".$p." AND location_ID = ".$loc; $result = $db->query($query); $result = $result->fetch_assoc(); return $result["area_ID"]; } function get_employee($request, $area, $db){ $data = $request->getRequestVars(); $point = htmlspecialchars($data["point"]); $location = htmlspecialchars($data["location"]); $query = "SELECT device_ID, phone_Type, employee_Phone FROM active_employees WHERE area_ID = ".$area." AND location_ID = ".$location; $result = $db->query($query); if($result->num_rows > 1){ $employees = array(); while($row = $result->fetch_assoc()){ array_push($employees, $row); } return $employees; } if($result->num_rows == 1){ return $result->fetch_assoc(); } return false; }
是否有可能记录应该由 php 处理的请求?信息太低,无法在这里给出有意义的答案。
我认为你得到的是一堆PHP代码,因为你发送的任何数据都没有插入到数据库中。 我认为可能有两个原因。
发送该数据时出现问题或接收数据时出现问题
使用类似于以下代码的 post 方法
NSString *string=@"Your url";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *mpfBoundry = [NSString stringWithString:@"---------------------------"];
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded; boundary=%@", mpfBoundry];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString* body =[NSString stringWithFormat:@"column1=%@&column2=%@&column3=%@", data1,data2,data3];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];