成功完成服务器响应并成功插入数据库后,无法获取服

时间:2013-02-21 14:40:23

标签: iphone ios xcode httpresponse

- (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`

1 个答案:

答案 0 :(得分:0)

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

很可能receivedData未正确定义,因此始终为nil,或者未正确保留,因此在您到达-connectionDidReceiveResponse:之前,它已被解除分类