ASIHTTPRequest Get Request - Delegate Not Getting Called - ios

My request is
NSURL *requestUrl = [NSURL URLWithString:url];
if(request_) {
[request_ setDelegate:nil];
[request_ release];
}
request_ = [[ASIFormDataRequest requestWithURL:requestUrl] retain];
[request_ setDelegate:self];
[request_ setRequestMethod:#"GET"];
[request_ setTimeOutSeconds:HTTP_TIME_OUT];
[request_ startAsynchronous];
The call is going since the network access indicator on the top bar is rotating and disappearing after a few seconds. But either of the delegate methods
- (void)requestFinished:(ASIHTTPRequest *)request
or
- (void)requestFailed:(ASIHTTPRequest *)request
is not being called. What could have went wrong?
Thanks in advance.
PS: Sometimes, due to some issue, server is returning a "500 Internal Server Error". But even in that case, shouldn't the delegate
- (void)requestFailed:(ASIHTTPRequest *)request
be called?

you can use the blocks to check that out.
here is the example of the same:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];
I hope this might help.

Make sure your class is conforming to the ASIHTTPRequestDelegate protocol.
#interface YourClass : ... <ASIHTTPRequestDelegate>

Related

How to post text message to server using ASIHTTPREQUEST handler in iOS?

I am create one comment screen with one textFileds and buttons when I enter some text in textfield and press buttons that text should post on my sever . my request is started but my textField value not display . give me this message.
2014-10-31 14:01:29.314 SMSProject[5894:137428] request started...
2014-10-31 14:01:29.922 SMSProject[5894:137428] Response 200 : "Invalid Request"
I am using this API.
-(void)sendRequest
{
deatilinfoarray = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:
#"http://sms.instatalkcommunications.com/apireq/AddCommentForSMS"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:#"Id" forKey:#"Id"];
[request setPostValue:commenttext forKey:#"Comment"];
[request setPostValue:#"Author" forKey:#"Author"];
[request setPostValue:#"SMSId" forKey:#"SMSId"];
[request setPostValue:#"CreatedDate" forKey:#"CreatedDate"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void) requestFinished:(ASIHTTPRequest *)request
{
NSLog(#"Response %d : %#", request.responseStatusCode, [request responseString]);
//NSData *responseData = [request responseData];
}
- (void) requestStarted:(ASIHTTPRequest *) request
{
NSLog(#"request started...");
}
- (void) requestFailed:(ASIHTTPRequest *) request
{
NSError *error = [request error];
NSLog(#"%#", error);
}
- (IBAction)btnpost:(id)sender
{
[self sendRequest];
}
I am not getting how to code for that please suggest me.
Try This:
Reference:
Tutorial
ASIHttpRequest POST
Source:
code

ASIFormDataRequest POST Request Issue

im using this code to post data to my server
NSURL *mainurl = [NSURL URLWithString:#"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];
NSString * postdata = [[NSString alloc]initWithFormat:#"UniqueId=%#&jsonProduct=%#&BranchId=%#&OrderToTime=%#",GETUnicidentifire,JsonOrderDetail,BranchId,OrderToTime];
ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];
[requestt setRequestMethod:#"POST"];
[requestt addRequestHeader:#"application/x-www-form-urlencoded" value:#"Content-Type"];
[requestt appendPostData:[postdata dataUsingEncoding:NSUTF8StringEncoding]];
NSString * theurl = [NSString stringWithFormat:#"%#",mainurl];
NSURLRequest *thereqest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:theurl]];
[NSURLConnection connectionWithRequest:thereqest delegate:self];
in the method
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"%#",error);
}
im geting:
{Message:The requested resource does not support http method 'GET'.}
what im doing Wrong ?
You are mixing things up here. You build an ASIFormDataRequest, but you don't actually send it. What you do send is an NSURLConnection.
It's been a long time since I've used ASI, but this might help:
NSURL *mainurl = [NSURL URLWithString:#"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];
ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];
[requestt addPostValue:GETUnicidentifire forKey:#"UniqueId";
[requestt addPostValue:JsonOrderDetail forKey:#"jsonProduct";
[requestt addPostValue:BranchId forKey:#"BranchId";
[requestt addPostValue:OrderToTime forKey:#"OrderToTime";
[requestt setCompletionBlock:^{
// Process the response
}];
[requestt setFailedBlock:^{
NSError *error = [requestt error];
NSLog(#"%#",error);
}];
[requestt startAsynchronous];
As a word of advice, replace ASI with something like AFNetworking. ASI is no longer being developed.

ios - ASIHTTPRequest and HUD Loading View Start and Stop

I have successfully implemented the ASIHTTPRequest framework to download a file from a URL and it saves in my Document Directory - YIPPIE!!!
Now I would like to show a Progress of some sort, so that the user can what is going on.
With the UIWebViewDelegate there are the following methods - I was hoping to use something similar.
-(void)webViewDidStartLoad:(UIWebView *)webView
-(void)webViewDidFinishLoad:(UIWebView *)webView
Is there something similar to use with ASIHTTP??
Here is an example mixing MBProgressHUD and ASI.
The idea is you start your updating before the begin the request and you end the updating in either the completion or failed blocks.
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Updating…";
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
}];
[request startAsynchronous];

ASIHTTPRequest delegation

im using ASIHTTPRequest to call webservice (soap):
-(void)callWebService:(NSString*)URL:(NSString*)SOAP{
NSURL *url = [NSURL URLWithString:URL];
NSString *SOAPMessage = [NSString stringWithFormat:SOAP];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.shouldAttemptPersistentConnection = NO;
[request setValidatesSecureCertificate:NO];
[request setRequestMethod:#"POST"];
[request appendPostData:[SOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setDidFinishSelector:#selector(requestCompleted:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
-(void)requestCompleted:(ASIHTTPRequest * )r{
NSString *responseString = [r responseString];
NSLog(#"%#",responseString);
}
-(void)requestFailed:(ASIHTTPRequest * )r{
NSError *Err = [r error];
NSLog(#"%#",Err);
}
If i call this in appDelegate.m, it works fine, requestCompleted handler throws the response...But when i use this same code in my own class it throws BAD ACCESS error, which i figured tells me i cannot delegate:self to handle response. if i setDelegate to appdelgate pointer (passed as ID sender) it works (and have handlers there). So why cant my own class handle its own events ? Im new to objective-c so i guess im missing something major here. Thanks
You have to have the requestCompleted and requestFailed in your "own class". Also that class has to live which means it can't be released while the service is being called. You have to save the instance of "your own class" in a strong/retained property or something.
Add this code to dealloc or viewWillDisappear
[request setDelegate:nil];
Check if your self is getting released while the delegate call happens. Make sure it is retained properly.

How can i setup socks proxy for ASIHTTPRequest in xcode ios?

i want to UIWebView transmit all data through socks proxy i already setup proxy url,port,username and password for ASIHTTPRequest but it doesn't work and i dont want to use pac file.
$- (IBAction)Go:(id)sender {
NSURL *url = [NSURL URLWithString:[TextURL.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url];
[request1 setProxyHost:#"test.org"];
[request1 setProxyPort:808];
[request1 setUsername:#"user"];
[request1 setPassword:#"password"];
[request1 setDelegate:self];
[request1 startAsynchronous];
[TextURL resignFirstResponder];
}
$- (void)requestFinished:(ASIHTTPRequest *)request{
NSData *responseData = [request responseData];
NSError *error = [request error];
if (!error) {
[web1 loadData:responseData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[request url]];
}
}

Resources