2013-02-21 4 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, Статус Описание: OK ID = 77

Or only ID number, for Example: `123` 

ответ

0

Где receivedData определяется? Это ивар? Как выглядит ваш журнал в -connection:didReceiveData:? recievedData показать данные в журнале? Если да, то что он очищает к моменту, когда вы доберетесь до -connectionDidReceiveResponse:?

По всей вероятности, либо receivedData не определен должным образом, всегда nil, либо не сохраняется должным образом, поэтому оно было dealloc'd, прежде чем попасть -connectionDidReceiveResponse:

+0

Я не какой-либо ответ от соединения» : didReceiveData, это показывающий [DO :: didReceiveData] 119119408 операции [не DO :: didReceiveData] DDB: 0,00, WDB: 0,00, соотношение: наны Полученный код состояния: 200 нет ошибки soapMsg1:. 0 DONE полученных байт: 0 theXML: –

Смежные вопросы