2012-04-10 50 views
0

我無法讓iPhone應用程序正確地使用我的API發送POST。撥打電話時,根本沒有結果返回。POST調用不會將REST API的結果返回到iPhone應用程序

  1. 的POST調用旨在發送消息(存儲的信息,覈對一些參數,並啓動「推」通知)

  2. 的電話後從形式適用於以.php文件,但不通過應用程序時使用。 返回結果:{「message」:{「message」:{「saved」:1}},「pressed」:1,「error」:[]}推測期間的錯誤是預期的,因爲test2.php不是發送消息到移動設備,只是數據庫。

  3. 下面是應用

    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]; 
    

什麼可能會造成應用程序加載的index.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. 

該請求具有以下要求:

  • 位置(號碼)
  • 點(數字)
  • 號(數字)
  • 消息(字符串)
  • 發送(日期時間:YYYY-MM-DD HH:MM:SS)

這裏的一個PHP代碼SAN的安全部分,以減少長度。

  1. ,處理一切開始的情況下=後的呼叫(將得到的是檢索消息)

    $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; 
    } 
    
  2. 而這裏的配套功能:

    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; 
    } 
    

回答

0

是有可能記錄應該由PHP處理的請求嗎?這裏信息太少,無法給出有意義的答案。

+0

感謝您的答覆POST方法,我在編輯中添加日誌從我的開發人員儘管它不包含非常多的信息。如果有幫助,我還添加了處理POST調用的PHP文件的很大一部分。讓我知道我是否可以提供任何可能有用的信息。 – user1324246 2012-04-10 16:52:49

0

我想你得到的是一堆php代碼,因爲無論你發送的數據是不是插入到數據庫中。我認爲可能有兩個原因。

問題發送數據或有問題的接收數據

使用如下代碼

NSString *[email protected]"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]; 
相關問題