2012-05-29 95 views
1

我遇到了問題NSURLConnectionNSURLConnection無法在設備上工作

我將請求發送到使用服務器:

[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

,但我得到一個「連接超時」的錯誤。

如果我使用Wi-Fi連接,它工作得很好。我的手機上有一個完整的3G網絡,可以以相當快的速度訪問其他應用和網站。

我不明白爲什麼我的請求沒有被髮送到服務器。

請幫忙。

我只是想:

[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES ]; 

但這不是工作要麼。

回答

1

是否有使用NSURLConnection的任何具體的原因,

我有類似的問題,並能夠利用的NSURLRequest的參考下

-(NSString *)sendFile:(NSString*)url File:(NSString*)pFileName Handler:(NSObject*) sender{ 


    NSString *base64Data = [AppUtil compressAnd64EncodedString:pFileName]; 

    NSMutableString* requestURL = [[NSMutableString alloc] init]; 
    [requestURL appendString:url]; 

    NSMutableString* requestBody = [[NSMutableString alloc] init]; 
    [requestBody appendString:@"request="]; 
    [requestBody appendString:@""]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithString:requestURL]]]; 

    // assume we are going to have data of 100 kb 
    NSMutableString *pRequestBody = [[NSMutableString alloc]initWithCapacity:100*1024]; 

    // append 
    [pRequestBody appendString:SOAP_START_PACKET]; 

    [pRequestBody appendString:base64Data]; 

    [pRequestBody appendString:SOAP_END_PACKET]; 

    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/soap+xml" forHTTPHeaderField:@"content-type"]; 
    [request setValue:@"utf-8" forHTTPHeaderField:@"charset"]; 

    NSData *requestData = [NSData dataWithBytes: [pRequestBody UTF8String] length: [pRequestBody length]]; 

    [request setHTTPBody: requestData]; 

    [request setTimeoutInterval:HTTP_TIME_OUT]; 

    NSError  *error = nil; 
    NSURLResponse *response = nil; 

    debugLog<<" sending request "<<endl; 

    NSData *pData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    if(pData){ 
     NSString *pRespString = [[[NSString alloc] initWithData:pData 
                 encoding:NSUTF8StringEncoding] autorelease]; 

     debugLog<<"Response of length = "<<(int)[pRespString length]<<endl; 

     // write file depending upon the response 
     NSString *pAppFileName = [self parseAndGenerateFile:pRespString]; 

     // done with the data 
     // [pData release]; 

     if(!pAppFileName){ 

      [self setErrorString:@"Server Error"]; 

      return nil; 

     } 

     return pAppFileName; 


    }else{ 
     debugLog<<" data is NULL"<<endl; 

     NSString *pErrorMessage = [error localizedDescription]; 

     [self setErrorString:pErrorMessage]; 

     /* Must be the Network error, lets show it to the user */ 

    } 
    return nil; 




} 
代碼
相關問題