2013-02-21 110 views
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:[email protected]/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,状态说明:行ID = 77

Or only ID number, for Example: `123` 

回答

0

在哪里receivedData定义?它是伊娃吗? -connection:didReceiveData:你的日志记录是什么样的? recievedData会在日志中显示数据吗?如果是这样,到-connectionDidReceiveResponse:的时候有什么结果呢?

在所有的可能性,无论是receivedData没有定义适当所以总是nil,或者没有被正确地保留下来,所以你能-connectionDidReceiveResponse:

+0

之前它被dealloc'd我不从“有关的任何回应:didReceiveData,它显示[DO :: didReceiveData] 119119408 operation [DO :: didReceiveData] ddb:0.00,wdb:0.00,比例:nan 收到状态代码:200无错误 soapMsg1:0 DONE。收到字节:0 theXML: – 2013-02-21 15:15:03