我在正確獲取JSON時遇到問題。這是我如何查詢數據庫。發送HTTP POST請求錯誤
NSString *url = [NSString stringWithFormat:@"http://www.myURL.com/list?key=%@", myKey]
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection;
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
與
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
NSMutableData *receivedData = [[NSMutableData alloc] init];
[receivedData appendData:data];
NSString *stringr;
stringr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSLog(@"Get string %@", stringr);
NSError *error;
NSDictionary *thejson = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
NSLog(@"Get dict %@", thejson); ///THIS RESULTS NULL
}
從獲取字符串的NSLog我得到兩個響應。我解釋得更好。如果我在瀏覽器中輸入我的請求URL,我會看到整個JSON響應,而從我的NSLog中,我得到一部分響應,另一部分仍在同一個NSLog中,但比第一個NSLog時間長。
例如:2014年10月11日19:38:58.401 MYAPP [1607:365755]獲取字典(在這裏我得到我的JSON的第一部分)
2014年10月11日19:38:58.401 Myapp [1607:365755]獲取字典(這裏我得到我的JSON的第二個也是最後一部分)。
爲什麼我沒有得到一個和整個NSLog回覆?我確信我誤會了一些東西,但我真的不知道是什麼。謝謝。