AFNetworking 2 downloading JPEG via POST straight into a File - post

I am downloading jpeg images via POST request using AFNetworking 2.0:
[manager.httpManager.requestSerializer setValue:#"image/jpeg" forHTTPHeaderField:#"Accept"];
NSDictionary *parameters = #{#"filename": imageName};
manager.httpManager.responseSerializer = [AFImageResponseSerializer serializer];
AFHTTPRequestOperation *operation = [manager.httpManager POST:URL_BASE_IMG parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Image: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Showing the responseObject is not an issue. However, how can I specify a path so that the response object is downloaded straight into it? I have seen the use of streams but I do not know how to implement it here. Anybody? Thanks in advance

Related

access the web service and send data using iOS Objective C

I have string objects and image stored in nsdata. How can i send it to a server by accessing the url? I have seen examples .but its not working.Can someone tell me
Thanks in Advance!!
You could use AFNetworking to easily send string and image to server using url.
Here is a link of tutorial how to use AFNetworking framework.
AFNetworking Tutorial
AFNetworking is the best way to do that. and there are so much tutorials out there.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"username": _username.text,
#"password": passwordMD5
};
[manager POST:#"http://example.com/ws/test.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject); //This is the Response
}else{
//if you didnt get expected output then handle it here
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
//Handle any network errors here. ex: if internet disconnected.
}];
Here is an example how to upload an image with string objects as a dictionary to a server using AFNetworking.
- (void)uploadPhoto {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:#"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}

How to Upload Images Using AFNetworking directly from iPad app

I am totally new to iOS.I want to upload images to my server using AFNetworking.
I tried it but it still not done. Please Help me, thanks in advance
try with this its work for me
-(void) uplodeImages{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"url where to upload images"]];
    NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:#"cat1.png"]);
    NSDictionary *parameters = #{#"username": #"", #"password" : #""};
    AFHTTPRequestOperation *op = [manager POST:#"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:#"userfile" fileName:#"cat1.png" mimeType:#"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(#"Error: %# ***** %#", operation.responseString, error);
    }];
    [op start];
}

AFNetworking 2.0 not parse xml

I want to read a response from a web service.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFXMLParserResponseSerializer new];
[manager GET:#"http://openapi.aibang.com/search?app_key=f41c8afccc586de03a99c86097e98ccb&city=%E5%8C%97%E4%BA%AC&q=%E9%A4%90%E9%A6%86" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
But I only got <NSXMLParser: 0x15d85490> from Xcode log. It's not the right xml response.
The output is correct, on iOS there is only the NSXMLParser which is a sax parser and you will need to implement the sax parsing.
The object returned in the block is an instance of NSXMLParser and that is what you see in your log statement, with object you parse the data returned from the HTTP request. The passing it self is handled with the NSXMLParserDelegate.
If you want DOM parsing, you could use the ONO XML parser, it is made by on of the developers of AFNetworking and it a DOM Parser.
And Of course there is nice integration with AFNetworking:AFOnoResponseSerializer
EN: Because the responseObject is a NSXMLParser instance, you can't treat it like string!
You have to implement NSXMLParserDelegate to handler the xml parser progress!
CN: responseObject返回的是NSXMLParser实例,你必须实现NSXMLParserDelegate协议才能处理该xml字符串!骚年! :-)
solution 1: use NSXMLParser to parser
[manager GET:#"http://openapi.aibang.com/search?app_key=f41c8afccc586de03a99c86097e98ccb&city=%E5%8C%97%E4%BA%AC&q=%E9%A4%90%E9%A6%86" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
[XMLParser setShouldProcessNamespaces:YES];
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
solution 2: use GDataXML to parser (much easier)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:#"http://openapi.aibang.com/search?app_key=f41c8afccc586de03a99c86097e98ccb&city=%E5%8C%97%E4%BA%AC&q=%E9%A4%90%E9%A6%86" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:responseObject encoding:0 error:nil];
NSLog(#"%#",doc.rootElement);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

AFNetworking - get response content length

I am using a simple AFNetworking [AFHTTPRequestOperation -initWithRequest:] for downloading file, I would like to check for the response header "content length" before I can actually download the file.
How can I check for response content length in AFNetworking ?
Using expectedContentLength Property of NSURLResponse You can you can find length.
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"Content-lent: %lld", [operation.response expectedContentLength]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

AFNetworking 2.0 - How to download uiimage async

In ios using AFNetworking 2.0 how can I easily download a remote image asynchronously and cache it for future request of the same url? I'm looking for a convenient way to receive both error callbacks and successful ones.
Thanks
You can do
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", responseObject);
_imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Image error: %#", error);
}];
[requestOperation start];
also mentioned How to download image with AFNetworking 2.0?

Resources