2013-08-06 32 views
15

tôi gửi một yêu cầu bưu điện đến máy chủ của tôi như này .:Lỗi AFNetworking: Không thể hoàn tất thao tác. (Cocoa lỗi 3840.)

-(Produkt*)fetchProductByID:(int)idNumber 
{ 

NSString* command = @"fetchProduct"; 
NSString* number = [NSString stringWithFormat:@"%d",idNumber]; 
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys: 
           command, @"command", 
           number, @"id", 
           nil]; 

[self commandWithParams:params onCompletion:^(NSDictionary *json) 
{ 
    //result returned 
    NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0]; 
    NSMutableArray* dics = [[NSMutableArray alloc]init]; 

    if ([json objectForKey:@"error"]==NULL && [res count]>0) 
    { 
     //success 
     for(int i=0;i<[[json objectForKey:@"result"]count];i++) 
     { 
      res = [[json objectForKey:@"result"] objectAtIndex:i]; 
      [dics addObject:res]; 

      NSLog(@"SPECIAL FETCHED PRODUKT:%@",res); 
     } 
    } 
    else 
    { 
     //error 
     NSLog(@"error:%@",[json objectForKey:@"error"]); 
     res = [[json objectForKey:@"result"] objectAtIndex:0]; 
     NSLog(@"SPECIAL FETCHED PRODUKT:%@",res); 
    } 

}]; 

return NULL; 

} 

Nhưng tôi luôn nhận được lỗi này: lỗi: Các hoạt động không thể hoàn tất. (Cocoa lỗi 3840.) Mã index.php trông như thế này:

<? 
require("lib.php"); 
require("QMServerAPI.php"); 

header("Content-Type: application/json"); 

switch ($_POST['command']) 
{ 
    case "fetchAllProduct": //Different question 
    fetchAllProduct(); 
    break; 

    case "fetchProduct": 
    fetchProduct($_POST['id']); 
    break; 

    case "insertProduct": 
    insertProduct($_POST['name'], $_POST['artikelNr'], 
    $_POST['anleitung'], $_POST['image'], $_POST['editDate'], 
    $_POST['id']); 
    break; 

    case "updateProduct": 
    updateProduc($_POST['name'], $_POST['artikelNr'], 
    $_POST['anleitung'], $_POST['image'], $_POST['editDate'], 
    $_POST['id']); 
    break; 
} 
?> 

Ở đây tôi thực hiện lệnh .:

function errorJson($msg) 
{ 
    header('Content-type: application/json'); 
    print json_encode(array("error"=>$msg)); 
    exit(); 
} 

function fetchAllProduct() 
{ 
    //fetch all Products 
    $result = query("SELECT * FROM Produkte"); 
    print json_encode($result); 
} 

function fetchProduct($id) 
{ 
     //fetch specific product 
     $result = query("SELECT * FROM Produkte WHERE id = '122'"); 
     print jeson_encode($result); 
} 
    function insertProduct($name, $artikelNr, $anleitung, $image, 
       $editDate, $id) 
{ 

    } 

    function updateProduct($name, $artikelNr, $anleitung, $image, 
       $editDate, $id) 
    { 
    //update old product 
    } 
?> 

Và đây là mã cho chức năng truy vấn .:

<? 

    //setup db connection 
    $link = mysqli_connect("localhost","root",""); 
    mysqli_select_db($link, "QM"); 

    //executes a given sql query with the params and returns an array as result 
    function query() { 
    global $link; 
    $debug = false; 

    //get the sql query 
    $args = func_get_args(); 
    $sql = array_shift($args); 

    //secure the input 
    for ($i=0;$i<count($args);$i++) { 
     $args[$i] = urldecode($args[$i]); 
    $args[$i] = mysqli_real_escape_string($link, $args[$i]); 
    } 

    //build the final query 
    $sql = vsprintf($sql, $args); 

    if ($debug) print $sql; 

    //execute and fetch the results 
     $result = mysqli_query($link, $sql); 
    if (mysqli_errno($link)==0 && $result) { 

    $rows = array(); 

    if ($result!==true) 
    while ($d = mysqli_fetch_assoc($result)) { 
    array_push($rows,$d); 
    } 

    //return json 
    return array('result'=>$rows); 

} else { 

    //error 
    return array('error'=> $mysqli->error); 
} 
} 

Tôi không biết whats sai, bởi vì khi tôi chỉ cần chọn tất cả các sản phẩm nó hoạt động tốt. Lỗi chỉ đến khi tôi sử dụng WHERE.

Cảm ơn sự giúp đỡ của bạn.

Trả lời

19

Lỗi ca cao 3840 là lỗi phân tích cú pháp JSON (nếu bạn tìm kiếm trên Stack Overflow cho nó, bạn sẽ đi qua this existing question about).

Tôi khuyên bạn nên chạy JSON đầu ra của mình từ dịch vụ web thông qua trình xác thực JSON để kiểm tra xem nó có thực sự tuân thủ thông số kỹ thuật hay không.

+1

Xin chào, bạn đúng có một số bài đăng đề cập đến cùng một lỗi. Nhưng dường như không ai giải quyết được lỗi của tôi. Khi tôi kiểm tra mảng, tôi chỉ nhận được lỗi này, không có đối tượng nào khác ... Có cách nào để nắm bắt phản hồi từ máy chủ (chạy xampp trên mac, vì vậy nó là cục bộ) để kiểm tra đầu ra JSON không? Và như tôi đã nói nó hoạt động tốt khi tôi sử dụng SELECT * FROM Produkte –

+0

Tôi một lần nữa, tôi không biết những gì tôi thay đổi .. Tôi chỉ cần khởi động lại máy chủ và đột nhiên tất cả mọi thứ hoạt động như nó .... sry cho eo thời gian của bạn: ( –

Các vấn đề liên quan