0

iPhoneアプリがAPIを使用してPOSTを正しく送信するのに問題があります。呼び出しが行われると、結果はまったく返されません。

  1. POST呼び出しは、メッセージを送信することを目的としています(メッセージを保存し、いくつかのパラメーターをチェックし、「プッシュ」通知を開始します)

  2. ポストコールは.phpファイルのフォームから機能しますが、アプリから使用した場合は機能しません。結果を返します:{"message":{"message":{"saved":1}}、 "pushed":1、 "error":[]} test2.phpが送信していないため、プッシュ中のエラーが予想されますデータベースだけでなく、モバイルデバイスへのメッセージ。

  3. これがアプリのPOSTコードです

    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コードです。

  1. 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;
    }
    
  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;
    }
    
4

2 に答える 2

0

PHPで処理する必要があるリクエストをログに記録する可能性はありますか? ここで意味のある答えを出すには情報が少なすぎます。

于 2012-04-10T14:58:36.153 に答える
0

送信するデータがデータベースに挿入されていないため、取得するのはphpコードの束だと思います。2つの理由が考えられると思います。

そのデータの送信に問題があるか、データの受信に問題があります

以下のコードのような 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];
于 2012-04-10T17:44:55.680 に答える