0
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *jString = @"{\"block_face_line\" : [{\"longitude\" : -71.34345555,\"latitude\" : 42.7794343 },{\"longitude\" : -71.4473179666667,\"latitude\" : 42.7336227666667  },  {\"longitude\" : -71.4461721166667,\"latitude\" : 42.7321493333333  },{\"longitude\" : -71.4473179662267,\"latitude\" : 42.726227666667  } ],\"block_face_curb_side\" : \"LEFT\",\"block_face_collector_id\" : \"3\"}";

    NSLog(@"JSON : : : : %@",jString);

    NSURL *passurl = [NSURL URLWithString:[[NSString stringWithFormat:@"https://XXXX:XXXX@parkme.goldenware.com/cgi-bin/db.test?COMMAND=add&TABLE=block_faces&USER=1&SUBSYSTEM=json"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"passurl:%@",passurl);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
    [NSURL URLWithString:[passurl absoluteString]]
    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];

    NSLog(@"request:%@",request);

    mFinalData = [jString dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [jString length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:mFinalData];

    receivedData = [[NSMutableData alloc] init];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
    NSLog(@"Connection = %@", connection);

    if(connection)
    {
        self.receivedData = [[NSMutableData data] initWithCapacity:2048];
        NSLog(@"receive data:%@",self.receivedData);
        NSString *stringData= [[NSString alloc] initWithData:receivedData
                                                    encoding:NSUTF8StringEncoding];
        NSLog (@"result%@", stringData);
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection Failed To Response");
    self->operationFailed = YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//  if (!operationBreaked)
//      [self.receivedData appendData:data];
//    else
//    {
//      [connection cancel];
//      NSLog(@" STOP !!!!  Receiving data was stoped");
//  }

    NSLog(@"DONE1111");
    [receivedData appendData:data];
    NSLog(@"didreceivedata:%@",receivedData);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"[DO::didReceiveData] %d operation", (int)self);
    NSLog(@"[DO::didReceiveData] ddb: %.2f, wdb: %.2f, ratio: %.2f",
          (float)bytesReceived,
          (float)expectedBytes,
          (float)bytesReceived / (float)expectedBytes);

    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
        NSLog(@"Received status code: %d %@", [(NSHTTPURLResponse *) httpResponse statusCode],
              [NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse *) httpResponse statusCode]]) ;
    }
    NSLog(@"soapMsg1: %d", [receivedData length]);
    [receivedData setLength:0];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"DONE. Received Bytes: %d", [receivedData length]);
    NSString *theXML = [[NSString alloc] initWithBytes:[receivedData mutableBytes] length:             [receivedData length] encoding:NSUTF8StringEncoding];
    NSLog(@"theXML:%@",theXML);
    connection = nil;
    receivedData =nil;
    operationFinished = YES;
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

//
// connection:didReceiveAuthenticationChallenge:
//
// Show the authentication challenge alert to the user (or gives up if the
// failure count is non-zero).
//
- (void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)aChallenge
{
    [aChallenge.sender useCredential:[NSURLCredential credentialForTrust:aChallenge.protectionSpace.serverTrust] forAuthenticationChallenge:aChallenge];
    NSLog(@"Challenge Protection Space = %@",aChallenge.protectionSpace.host);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

获取服务器响应 200 但未获取响应字符串,来自服务器的响应应如下所示:

// 状态码:OK,状态描述:OK ID=77

Or only ID number, for Example: `123`
4

1 回答 1

0

在哪里receivedData定义?是ivar吗?您的日志记录是什么样的-connection:didReceiveData:?是否recievedData在日志中显示数据?如果是这样,当你到达的时候是什么清除它-connectionDidReceiveResponse:

很可能,要么receivedData没有正确定义,所以总是如此nil,或者没有被正确保留,所以在你到达之前它被释放了-connectionDidReceiveResponse:

于 2013-02-21T15:05:05.100 回答