2013-03-17 5 views
16

Sto usando AFJSONRequestOperation richiedere un server e analizzare la risposta JSON restituito, ma durante l'analisi, ho ottenuto questo errore:NSDebugDescription = "Il testo JSON non è stato avviato con array o oggetto e opzione per consentire ai frammenti non impostati.";

NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set."; 

ho controllato l'API ed è la restituzione dei dati JSON:

header('Content-type: text/json'); 
$arr[] = array("Message" => "update succeeded"); 
echo '{"Result":'.json_encode($arr).'}'; 

Qualche idea su come sistemarlo?

EDIT

Ho provato a chiamare l'API da browser e comprendono la richiesta nel URL, e così ho ottenuto una risposta valida JSON:

{"Result":[{"Message":"update succeeded"}]} 
+0

Potresti pubblicare la stringa JSON ricevuta da 'AFJSONRequestoperation'. –

+0

Salve, il JSON che ho inserito nel blocco di errore è '(null)' – Malloc

+1

puoi eseguire l'api nel browser e includere la risposta alla domanda? –

risposta

0

Prima cosa, json_encode l'intero oggetto piuttosto che irrompere in pezzi.

In secondo luogo, a meno che $ arr contiene elementi multipli (non chiaro da esempio di cui sopra), dovrebbe essere initalized come così:

$arr = array("Message" => "update succeeded");

io non sono ancora certo che altro potrebbe essere il problema qui. Dovresti fare eco a ciò che la tua app sta ricevendo e questo dovrebbe indicare il problema.

0

Utilizzare un tipo di contenuto accettabile. nel tuo webservice che dovrebbe essere solo testo normale.

Ecco il mio codice rapida e fisso:

let manager = AFHTTPRequestOperationManager() 

    manager.requestSerializer=AFJSONRequestSerializer() 
    manager.responseSerializer = AFHTTPResponseSerializer(); 

    manager.GET(

     baseURL + (webServiceType as String) + secureParam, 
     parameters:[:], 

     success: 
     { (operation: AFHTTPRequestOperation!, 
      responseObject: AnyObject!) in 
      completion(obj: responseObject) 
     }, 
     failure: 
     { (operation: AFHTTPRequestOperation!, 
      error: NSError!) in 
      completion(obj: nil) 
    }) 
0

Verifica d'aver aggiunto/api/prima del tuo URL di base delle API come

http: // someurl/yourBasrUrl/api/apiName

0

per dare una risposta jSON valido, il codice dovrebbe simile a questa:

$response = array(
    "Result" => array(
     "Message" => "update succeeded" 
    ) 
) 

echo json_encode($response); 
Problemi correlati