Iphone 对象引用未设置为XML中对象的实例
我不熟悉iPhone,在我的应用程序中使用XML,我需要将一个图像发送到服务器的一个特定文件夹。同样,我使用的代码是:Iphone 对象引用未设置为XML中对象的实例,iphone,objective-c,xml,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/P
NSString *filename = @"Image";
NSData *imageData = UIImageJPEGRepresentation([imageView image], 90);
NSString *urlString = [NSString stringWithFormat:@"http://111.111.11.1/webservices.asmx/PutImage"];
//网址是:
我在返回字符串中得到类似“对象引用未设置为对象实例”的消息。有人能为我提供一些解决方案吗?要将数据从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];
}
好的。。。请尝试我正在使用的代码 //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];
您的返回数据是empty@sicKo:您能帮我解释一下原因吗?在[request setURL:[NSURL URLWithString:[NSString STRING STRING WITHSTRING:@“如果我想传递我的参数,比如Img=image.png,该怎么办?我想,image.png将由nsdata获取,但该参数的作用是什么?我知道,但我问的是参数。我需要在URL中传递该图像。比如imgName=something.png,这与我使用的代码相同。我对[body appendData:[[NSString STRING WITHSTRING:@“内容配置:表单数据;名称=\“用户文件\”文件名=\”.jpg\“\r\n”]dataUsingEncoding:NSUTF8StringEncoding];我需要传递什么来代替名称=\“用户文件\”;文件名=\”。jpgname=您的NSData*图像数据和文件名=您的NSString*文件名
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];