对象引用未设置为对象的实例在XML中

时间:2011-11-10 05:26:31

标签: iphone objective-c xml

我是iPhone的新手,在我的应用程序中使用XML,我需要将一个图像发送到服务器的一个特定文件夹。同样我使用的代码:

NSString *filename = @"Image";
NSData *imageData = UIImageJPEGRepresentation([imageView image], 90);
NSString *urlString = [NSString stringWithFormat:@"http://111.111.11.1/webservices.asmx/PutImage"];

// url是:http://111.111.11.1/webservices.asmx/PutImage?ImgIn=image.jpg

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"--------1473780983"];
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images\"; filename=\"%@.jpg\"\r\n", filename] 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]];

[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"\n\n\nreturnString : %@",returnString);

我收到的消息如下:“对象引用没有设置为对象的实例”在返回字符串中。任何人都能指导我一些解决方案吗?

2 个答案:

答案 0 :(得分:0)

要将数据从iPhone上传到服务器:

  - (void)sendImage {

              NSData *postData = [nsdata from your original image];
              NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

              // Init and set fields of the URLRequest
              NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
              [request setHTTPMethod:@"POST"];
              [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];

              [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
              [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
              [request setHTTPBody:postData];

              NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

              if (connection) {
                    // Return data of the request
                       NSData *receivedData = [[NSMutableData data] retain];
               }
             [request release];

       }

答案 1 :(得分:0)

好的......试试我正在使用的代码......

// imageview是一个UIImageView

NSData *imageData = UIImageJPEGRepresentation(imageview.image, 100);


// setting up the URL to post to
NSString *urlString = @"http://example.com/upload";

// 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 stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] 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];
self.returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];