Set different callback with ASIHTTPRequest - iphone

For an app i'm currently making i use the ASIHTTPRequest API to do my communication:
NSURL *url = [NSURL URLWithString:#"http://testService.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setTimeOutSeconds:20.0f];
[request setRequestMethod:#"POST"];
NSData * postData = [NSJSONSerialization dataWithJSONObject:dictionnary2 options:0 error:nil];
[request setPostLength:[postData length]];
[request appendPostData:postData];
[request setDelegate:self];
[request startAsynchronous];
i already have working calls in place but they both go to the same callback method :
- (void)requestFinished:(ASIHTTPRequest *)request
I want each call to have it's own callback method since i call one call from the callback method of the other. How can i do this ?

In this kind for situation i would always prefer to use Blocks for call backs.
Check this link for block implementation methods and design,

Related

How to post xml data in url as request?

I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];

Log into website with ASIHttpRequest

I have a website that i want to log into from my iphone in a iphone app i am making, i followed the documentation on ASIHttpRequests website but all i get from the response string is the html code for the login page , but i get a OK http code, Why is this happening?
Here is my code :
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request shouldPresentCredentialsBeforeChallenge];
[request setRequestMethod:#"POST"];
[request setPostValue:#"" forKey:#"username"];
[request setPostValue:#"" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request setDidFailSelector:#selector(requestLoginFailed:)];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
NSString *yourResponse = [request responseString];
NSLog(#"%#", yourResponse);
}
The form on that page is doing a get, not a post. Your server is probably not expecting POST data, and is looking at query string params instead. Change to
[NSURL URLWithString:#"http://www.rssit.site90.com/login.php?username=YOURUSERNAME&password=YOURPASSWORD"];
and set the requestMethod to GET.

how to send post request with nsurlconnection

I have changed over to NSURLConnection and NSURLRequest delegates for my db connections etc.
I was using the ASIHTTPRequest libraries to do all this stuff but finally decided to move on due to the lack of support for that 3rd party library.
what I am woundering is how do I send post requests to my db like you do with the ASIFormDataRequest as shown below
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setValidatesSecureCertificate:NO];
[request setPostValue:#"ClientDataSet.xml" forKey:#"filename"];
[request startSynchronous];
i.e. how to do send the setPostValues with the NSURLRequest class?
any help would be greatly appreciated
Using NSURLRequest is slightly more difficult than utilizing ASIHTTPRequest. You have to build your own post body.
NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];

How do I use ASI Http in iOS to POST data to a web service?

I am using the instructions provided in the ASI page here. I am trying to send some data to a web service and not seeing any results.
This is my sendRequest method which gets called in viewDidLoad
-(void)sendRequest {
NSURL *url = [NSURL URLWithString:#"http://153.60.6.75:8080/BarcodePayment/transactions"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
NSString *dataContent = #"{\"id\":7,\"amount\":7.0,\"paid\":true}";
NSLog(#"dataContent: %#", dataContent);
[request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
}
I check the dataContent string and the output is
{"id":7,"amount":7.0,"paid":true}
If I use curl from Terminal, I checked and this command works.
curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'
My understanding is that in using curl, I set it to json, specify the address, specify the data which is equivalent to dataContent in my code. Nothing happens. What's wrong?
Thanks for the help!
You have pretty much everything except the most crucial component which is to start the request
You need to add [request startSynchronous]; for a Synchronous Request or [requester startAsynchronous]; for a Asynchronous request (and you possibly need the Delegate Methods to handle any response you have back)
This is all covered pretty nicely in the ASIHTTPRequest How to Use Guide. The Sections most relevant to this would be 'Creating a synchronous request' and 'Creating an asynchronous request'. Also something to think about taken from that page:
In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request.
You forgot to call:
[request setDelegate: self];
[request startAsynchronous];
Or:
[request startSynchronous];
If you don't call any of these, the request will never be made :)
I don't see a call to [request startSynchronous] (or [request startAsynchronous]) in your code... Are you even initiating the request anywhere?
It looks like you didn't call [request startAsynchronous];
Check ASIHTTPRequest documentation for more details on how to use
You're not actually sending the request.
1) You'll need to call [request startSynchronous] or [request startAsynchronous]
2) If you use asynchronous (which you should probably do) you'll need to set the delegate and implement a - (void)requestFinished:(ASIHTTPRequest *)request method.
- (void)postThresholdDetails:(NSDictionary *)info
{
NSString *urlString = [NSString stringWithFormat:#"%#%#/",BaseUrl,PostThresholdDetails];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:120];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"content-type" value:#"application/x-www-form-urlencoded"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
[request setPostBody:[NSMutableData dataWithData: [info objectForKey:#"jsondata"] ]];
[request startAsynchronous];
}

Is the ASIHTTPRequest is not asynchronous?

I am using ASIHTTPRequest to fetch some data from a web service.
I am making requests using a loop.
The problem is that it doesn't seem the request is going asynchronously so that my activityindicator is not working .
Is it true that ASIHTTPRequest is not asynchronous .
or should i use the regular nsmutablerequest to perform asynchronous request .
You should put your request in a download queue, i.e.
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:request];
[request release];
Just
[request startAsynchronous];
runs the request on the UI thread, so you should try it with download queue.
For Sync Synchronous
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
Creating an asynchronous request
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
And there is lot of more options like Queue and many more
You can refer http://allseeing-i.com/ASIHTTPRequest/How-to-use