Versuchen zu tun, eine einfache JSON-Anfrage in Objective-C (IOS7)

Ich habe die Suche und versuchen, viele verschiedene Lösungen, aber ohne Glück.

Ich versuche, ein JSON Request an meinen server, es funktioniert sicher, da ich es getestet habe in PHP (einfach anrufen, die es erhält, string name und string-Passwort und prüfen in der Datenbank, wenn die Benutzer richtig ist). Die Anforderung erhalten, sollte wieder ein JSON-Eigenschaft "Erfolg" nur (die "true" oder "false").

Hier ist mein code:

- (void)logonService:(NSString *) name widthArg2:(NSString *) password {
//Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://46.51.169.145/ios/index.php/user/login/"]];

//Specify that it will be a POST request
request.HTTPMethod = @"POST";

//setting json fields
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//prepare output data
NSString *in1;
NSDictionary *outputData = [NSDictionary dictionaryWithObjectsAndKeys:in1, @"success", nil];
NSError *error = nil;
NSData *jsonOutputData = [NSJSONSerialization dataWithJSONObject:outputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonOutputString = [[NSString alloc] initWithData:jsonOutputData encoding:NSUTF8StringEncoding];
//set json string to body data
NSData *requestInputBodyData = [jsonOutputString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: requestInputBodyData];

//prepare input data
NSString *input = [NSString stringWithFormat:@"&name=%@&password=%@",name,password];
//Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.
NSData *inputData = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//calculate length of post data
NSString *inputLength = [NSString stringWithFormat:@"%d",[inputData length]];
//Set HTTP header field with length of the post data.
[request setValue:inputLength forHTTPHeaderField:@"Content-Length"];
//encode type
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
//add it to http body
[request setHTTPBody:inputData];

//Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn){
    NSLog(@"Connection Successful");
}else{
    NSLog(@"Connection could not be made");
}

}

Den Delegierten DidReceiveData feuert korrekt, aber das Ergebnis ist immer NULL:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *jsonResponseData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"RESULT1: %@", jsonResponseData);
[_responseData appendData:data];

}

Vielen Dank, hoffe jemand kann mir helfen!

  • Sehen, dass "Fehler:" parm? Es ist für einen Grund. Es verwenden.
  • (Und bitte deutlich machen, "data" null", oder ist "jsonResponseData" null -?)
  • (Und warum sind Sie versuchen zu analysieren, die teilweise Ergebnis vom server vs zu warten, für die ganze Sache?)
  • Entfernen Sie alle JSON-Zeug und der download funktioniert. Wenn Sie es arbeiten, legen Sie die JSON-parsing zurück in connectionDidFinishLoading, nicht didReceiveData.
InformationsquelleAutor luis83rs | 2013-09-30
Schreibe einen Kommentar