我的帖子方法如下:
NSString *totalPostURL = [NSString stringWithFormat:@"%@registerDevice",self.textUrl];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:totalPostURL]];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:self.finalDict options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
代码工作正常,但我不知道如何打印这篇文章的回复。任何建议都欢迎,因为我是iOS开发的新手。
答案 0 :(得分:2)
您可以按以下方式打印回复:
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString);
但请记住,因为你是新手,不要忘记阅读调用webservices的教程。Interacting with webservices.
答案 1 :(得分:1)
您可以在连接委托中获得响应。寻找方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
答案 2 :(得分:1)
你可以这样使用......
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonString = [[NSString alloc] initWithString: receivedData];
NSData* cData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *WSerror;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:&WSerror];
}
打印responseDic。
答案 3 :(得分:0)