2016-05-06 17 views
0

JSONを介して2つの変数(携帯番号と顧客キー)を要求するこのコードを書くと、2つの変数(IDと検証済み)で応答を受け取りたいのですが、なぜ私は知らないのですか?swiftのJSONからの応答を読み込みます。

次のような登録ボタン内部のこの部分のための私のコード:

  do { 

      let post:NSString = "dst=\(completenumber)&customer_key=\(self.CUSTOMER_KEY)" 



      NSLog("PostData: %@",post) 

      let regURL:NSURL = NSURL(string: "myreg URL here")! 


      let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)! 

      let postLength:NSString = String(postData.length) 

      let request:NSMutableURLRequest = NSMutableURLRequest(URL: regURL) 
      request.HTTPMethod = "POST" 
      request.HTTPBody = postData 
      request.setValue(postLength as String, forHTTPHeaderField: "Content-Length") 
      request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
      request.setValue("application/json", forHTTPHeaderField: "Accept") 

      var reponseError: NSError? 
      var response: NSURLResponse? 

      var urlData: NSData? 

      do { 
       urlData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response) 
       // print(urlData) 
      } catch let error as NSError { 
       reponseError = error 
       urlData = nil 
      } 

      if (urlData != nil) { 

       let res = response as! NSHTTPURLResponse!; 

       NSLog("Response code: %ld", res.statusCode); 

       if (res.statusCode >= 200 && res.statusCode < 300) 
       { 
        let responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)! 

        NSLog("Response Data ==> %@", responseData); 

        //var error: NSError? 

        let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary 


        let success:NSString = jsonData.valueForKey("verified") as! NSString 
        let resultid:NSString = jsonData.valueForKey("id") as! NSString 

        //[jsonData[@"success"] integerValue]; 

        NSLog("Verified: %@", success); 
        NSLog("ID: %@", resultid); 

        if(success == "new") 

        { 
        NSUserDefaults.standardUserDefaults().setObject(resultid, forKey: "VerifiedID") 


        NSUserDefaults.standardUserDefaults().synchronize() 

        self.dismissViewControllerAnimated(true, completion: nil) 

        self.performSegueWithIdentifier("enter_pin", sender: self) 
        } 


       } 

      } else { 
       let alertView:UIAlertView = UIAlertView() 
       alertView.title = "Registration Failed!" 
       alertView.message = "Connection Failure" 
       if let error = reponseError { 
        alertView.message = (error.localizedDescription) 
       } 
       alertView.delegate = self 
       alertView.addButtonWithTitle("OK") 
       alertView.show() 
      } 

      } catch { 
       let alertView:UIAlertView = UIAlertView() 
       alertView.title = "Registration Failed!" 
       alertView.message = "Server Error!" 
       alertView.delegate = self 
       alertView.addButtonWithTitle("OK") 
       alertView.show() 
      } 

エラー:

Response code: 200 
Response Data ==> Error 

(更新)JSONファイル:

<?php 
    require("lib.php"); 
    require ("src/mobile_verify_lib.php"); 
    require_once 'plivo.php'; 
    require_once dirname(__DIR__) . '/core/logentries/logentries.php'; 
    date_default_timezone_set('GMT'); 
    header('Content-Type: application/json'); 


if (!$_GET[customer_key]) 
    { 
    echo "Error"; 
    exit; 
    } 
$kkey = $_GET[customer_key]; 
$check_customer = check_customer($kkey); 
$customer_id = $check_customer[id]; 

$src = "19178180011"; 
$dst = $_GET[dst]; 
#$text = $_GET[text]; 


$pin = getpin(); 

$vid = generateRandomString(16); 

$checktel = checktel($dst,$customer_id); 

if($checktel[status]) 
    { 

    #echo "You already have a pending verification request<br>"; 
    if($_GET[clear] == "yes") 
    { 
    destroyvid($checktel[vid]); 
    $js[id] = $checktel[vid]; 
    $js[verified] = "cleared"; 
    echo json_encode($js); 
    exit; 
    } 
    destroyvid($checktel[vid]); 

} 


//Check mobile number format and type (Only Mobile type is accepted) 
$test = check_mobile($dst); 

if(!$test[valid]) 
    { 
    $js[id] = "NONE"; 
    $js[verified] = "INVALID"; 
    echo json_encode($js); 
    #echo '{"id":"NONE","verified":"INVALID"}'; 
    exit; 
    } 

    // End 


    $dateandtime = date("Y-m-d H:i:s"); 



$servername = "mysql.tmed.pw"; 
$username = "ahmad"; 
$password = "ahVb$4TrHA11"; 
$dbname = "acticonnectverify"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 

$sql = "INSERT INTO phone_verification (vid, mobile_number, pin, customer_id,dateandtime) VALUES ('$vid', '$dst', '$pin', '$customer_id' , '$dateandtime')"; 

if ($conn->query($sql) === TRUE) { 
    # echo "New record created successfully"; 
    } else { 
    # echo "Error: " . $sql . "<br>" . $conn->error; 
} 

    $conn->close(); 


$text = "To verfiy your mobile number, Please enter this PIN: $pin"; 
$js[id] = $vid; 
$js[tel] = $dst; 
$js[text] = "true"; 
$js[verified] = "new"; 


echo json_encode($js); 
#echo '{"id":"' . $vid . '","tel":"' . $dst .'","text":true,"verified":"new"}'; 

sendsms($dst,$src,$text,$customer_id); 


?> 
+0

私はエラーで私のポストを更新しましたが、たぶんあなたのサーバが別の身体を期待し、それが会社のサーバー側にありますが、私は –

+0

をしたいと私はアンドロイドのアプリで同じJSONを使用して、それが良い働いているので、JSONについて、それは私ではありません。 "エラー"は接続エラーではなく、サーバーからの応答です。 – Daniel

+0

OKこれを再度確認し、ここに構文を入力してください。 –

答えて

0

私がすることで、この問題を解決これらの行をコメントし、ポストデータを使用せずに通常の方法で完全なURLを渡す

//let post:NSString = "dst=\(completenumber)&customer_key=\(self.CUSTOMER_KEY)" 
//let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)! 
//let postLength:NSString = String(postData.length) 
//request.HTTPBody = postData 
//request.setValue(postLength as String, forHTTPHeaderField: "Content-Length") 
関連する問題