I know there are tons ways to POST from an iOS app from nsurlconnection to asihttprequest.
What is the preferred or best practices way to post a few variables, asynchronously, to an end point, in iOS7?
My endpoint can be invoked in the following fashion:
curl -d 'username=tt&email=tt#example.com' 127.0.0.1/register
I ended up using afnetworking. This method works real nice.
-(void) postDictionary:(NSDictionary *)data toEndpoint:(NSString *)endpoint{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:endpoint parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
Related
Anyone know. how can make service call during user interacts with particular screen. I mean I need to call additional data while user interact with app. but it still look like device is hang. help me please.
For Get request use this code
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
or
NSURL *URL = [NSURL URLWithString:#"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
As you have not mentioned any code snippet, I assume that you are performing all your operations on main thread and that is the reason why your app is hanging. To be specific about main thread and background thread, we perform all the UI related tasks on main thread and background tasks on some other thread.So you can create another thread for your background task like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your background task here
});
in my app I'm using the new AFN 3.0 and I have
AFHTTPSessionManager *manager
instead of
AFHTTPRequestOperation *operation
my problem is that before I was able to get some data from RequestOperation as:
NSURL *url = operation.request.URL;
//or
NSNumber statusCode = operation.response.statusCode;
//or
NSData *responseData = operation.responseData;
and how can I get this elements with AFHTTPSessionManager?
thanks
in v2 you were getting AFHTTPRequestOperation for the request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
But in the v3 you will get NSURLSessionTask
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
So based on that you can get the details the from the NSURLSessionTask like the currentRequest , response etc
For more changes and details, you can refer to the migration guide of AFNetworking
AFNetworking Migration Guide
For NSURLSessionTask Reference : NSURLSessionTask
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];
}
I'm using AFNetworking 2.4.1 to fetch YouTube thumbnails and the thing is I get empty cells when I use YouTube v3. I have no problem with v2.
What seems to be the problem in the below code?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&key={YOUR_API_KEY}" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
_objects = [[responseObject objectForKey:#"data"]objectForKey:#"items"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Thanks in advance!
I'm having some trouble with RESTfull web service and I'm trying to see my request as a text using NSLog. I tried this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
...
[manager POST:urlString parameters:mutableParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Response: %#", [operation description]) ;
if (block) {
block(error);
}
NSLog(#"-------------------------------");
NSLog(#"Request: %#", manager.requestSerializer.debugDescription);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", manager.requestSerializer.description);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", operation.request.HTTPBodyStream);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", operation.request);
NSLog(#"-------------------------------");
NSLog(#"Error: %#", error);
NSLog(#"-------------------------------");
}];
Is there any way to NSLog the request from AFHTTPRequestOperationManager (AFNetworking 2) ?
Have a look to the POST method:
[manager POST:urlString parameters:mutableParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
You have a AFHTTPRequestOperation *operation and an id responseObject in the success block.
The best thing that you can do is to put a:
NSLog(#"AFHttpRequestOperation %#", operation);
and set there a breakpoint so you can inspect what's happening there:
What do you want to see about the operation's request?
Not exactly logging but this answer explains how to convert NSURLRequest to NSString