2012-08-02 189 views
1

我正在應用程序。我想發送圖像到服務器,但我的解析沒有調用,並沒有得到任何response.i嘗試下面的代碼。圖像發送問題到服務器

-(void)callParsing 
{ 

NSData *imgData=UIImageJPEGRepresentation(cameraImageView.image,0.5); 


    NSLog(@"%@",imgData); 

    NSString *regestrationString=[[NSString alloc]initWithFormat:@"http://humsomething.netsmartz.us/HumServices.svc/updateimage/vchImage=%@",imgData]; 

    NSURLRequest *regestrationRequest=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:regestrationString]]; 

    NSURLResponse *regestrationResponce; 

    NSError *error; 

    NSMutableData *regestrationData=[[NSMutableData alloc]initWithData:[NSURLConnection sendSynchronousRequest:regestrationRequest returningResponse:&regestrationResponce error:&error]]; 

    //[self performXML:regestrationData]; 
    results = [[NSMutableArray alloc] init]; 
    xmlParser = [[NSXMLParser alloc] initWithData:regestrationData]; 
    [xmlParser setDelegate: self]; 
    [xmlParser setShouldResolveExternalEntities: YES]; 

    if([xmlParser parse]) 
    { 
     NSLog(@"Sucessful"); 
    } 
    else 
    { 
     NSLog(@"Failure"); 
    } 

} 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName 
    attributes: (NSDictionary *)attributeDict 

{   

    if([elementName isEqualToString:@"RegisterUserResult"]) 
    { 
      if(!soapResults) 
      { 
       soapResults = [[NSMutableString alloc] init]; 
      } 
      [soapResults setString:@""]; 
      recordResults = TRUE; 
     }  
} 


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{  
    if(recordResults) 
    { 
     [soapResults appendString:string]; 
     NSLog(@"%@",soapResults); 
    } 
} 

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 

    if([elementName isEqualToString:@"RegisterUserResult"]) 
    { 
     [results addObject:soapResults]; 
      NSLog(@" REPONSE %@",results); 
    } 
+0

你可以添加更多的細節,如什麼動作調用callParser()?如果([xmlParser parse]) { NSLog(@「Sucessful」);如果([xmlParser parse]) – 2012-08-02 12:17:47

+0

有您看到該行的所有代碼 – 2012-08-02 12:20:11

+0

; } 這行調用解析器,但其結果失敗 – 2012-08-02 12:20:46

回答

0

我正在使用以下作爲我的數據的上傳器。

-(void) Uploader:(NSData *) imageData fileName : (NSString *) Name 
{ 

    NSLog(@"%d",[imageData length]); 

    NSString *urlString = @".aspx URL"; 

    // setting up the request object now 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    /* 
    add some header info now 
    we always need a boundary when we post a file 
    also we need to set the content type 

    You might want to generate a random boundary.. this is just the same 
    as my output from wireshark on a valid html post 
    */ 

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    /* 
    now lets create the body of the post 
    */ 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",Name] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 


    // now lets make the connection to the web 
    //NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    //NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    theConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 
    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 

    uploadedData = 0.00; 
    totalData = [imageData length]; 


}