2013-08-06 6 views
15

mi invia una richiesta POST al mio server come questo .:AF Errore di rete: impossibile completare l'operazione. (Errore di cacao 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; 

} 

Ma ho sempre ottenere questo errore: Errore: L'operazione non ha potuto essere completato. (Errore di cacao 3840.) Il codice index.php è simile al seguente:

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

Qui ho eseguire il comando .:

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

E qui è il codice per la funzione di query .:

<? 

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

Non so cosa c'è di sbagliato, perché quando seleziono tutti i prodotti funziona correttamente. L'errore arriva quando uso WHERE.

Grazie per il vostro aiuto.

risposta

19

Errore di cacao 3840 è un errore di analisi JSON (se si ricerca su Stack Overflow per questo si incontra this existing question about).

Suggerisco di eseguire il JSON di output dal servizio Web tramite un validatore JSON per verificare che sia effettivamente conforme alle specifiche.

+1

Ehi, hai ragione, ci sono diversi post che si riferiscono allo stesso errore. Ma nessuno sembra risolvere il mio errore. Quando controllo l'array, ottengo solo questo errore, nessun altro oggetto ... C'è un modo per intercettare le risposte dal server (eseguendo xampp su mac, quindi è locale) per controllare l'output JSON? E come ho detto funziona bene quando uso SELECT * FROM Produkte –

+0

Me di nuovo, non so cosa ho cambiato .. Ho appena riavviato il server e all'improvviso tutto funziona come dovrebbe .... sry per waisting il tuo tempo :( –