Using NSURLConnection WebService - iphone

i get the follwing error below when i run the program given below
Error:
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x5948b80 {NSUnderlyingError=0x5948ac0 "bad URL", NSLocalizedDescription=bad URL}
wat i need to do plz suggest me
thank u..
the code is given below
#implementation WebSampleViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dataWebService = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#" http://www.googleapis.com/customsearch"]] retain];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(#"Response: %#",responseString);
[responseString release];
[dataWebService release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Error during connection: %#", [error description]);
}

You should not retain request instance of type NSMutableURLRequest.
You have an leading white space in your String URL.
Use the below code .
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.googleapis.com/customsearch"]];
I think, you should check your URL , I give the same "Not Found" as on iPhone.

Try it without the space at the beginning of your URL string.

You have to remove whitespace in your url

Related

Returning a string from an NSURLRequest

Is anybody able to help me get a string returned from an NSURLRequest on an iPhone app? I have to send some user credentials to a URL and it will return a customer ID number. That is what I need to be pulled in as a string, I am not used to doing working with servers or HTTP requests, so any help will be great. I have already read the Apple Docs about it and am a bit lost on this part.
This is a standard pattern async downloader:
In .h file:
NSMutableData *responseData;
And .m file:
-(void) execute {
NSString *urlString = #"http://www.google.com";
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[responseData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
[connection release];
NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];
NSLog(#"%#", data);
[responseData release];
}
This will download Google and outprint the content.

rest web services in iphone

I have a problem now. I need to pass an transactionID and an user password to a rest service and it is suppose to return me a true/false value (in XML format). However, it is consistently returning me (null).. I am totally lost some one please help.
NSString *urlString = [NSString stringWithFormat:#"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%#&password=%#", _transactionID.text, _userPassword.text];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSString *result = [[NSString alloc] initWithContentsOfURL:url];
NSLog(#"%#",result );
My result is constantly returning me null. How do i continue from here?
.h:
NSMutableData *responseData;
.m:
- (void)load {
NSURL *myURL = [NSURL URLWithString:#"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%#&password=%#", _transactionID.text, _userPassword.text];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
[textView setString:#"Unable to fetch data"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData
`enter code here` length]);
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
Consider using NSURLConnection which has a callback for the result and also a callback to get detailed error details. It als doesn't execute on the UI thread (doesn't hang UI during the request).
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Then you can implement the delegate methods to get the error, the data and other details:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"result: %#", responseString);
[responseString release];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"error - read error object for details");
}
You might try using "initWithContentsOfURL:encoding:error:" instead and check "error". Also, use Charles or other http sniffer and compare results to a straight browser request ( did you check results in a browser?)

Inconsistent results from NSURL

Using the following code to make my requests, what i've noticed is that data will sometimes not be loaded or fully loaded, this always happens when the app first starts. Any thoughts? I've tried putting in [tableView refresh] in the connectionDidFinishLoading, but that doesn't seem to help, any ideas? Thanks for looking
.h:
NSMutableData *responseData;
.m:
- (void)load {
NSURL *myURL = [NSURL URLWithString:#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
[textView setString:#"Unable to fetch data"];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData
length]);
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}
Are you sure your data did not load? Try [tableView reloadData]; instead of refresh.

how to handle the responseString from json output and parse it

I am using the following code and i am getting a response string in JSON. I don't know how to parse it. What should I do next if I want to call any method on JSON output
// source code
- (void)viewDidLoad
{
[super viewDidLoad];
dataWebService = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse"]]retain];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(#"Response: %#",responseString);
[responseString release];
[dataWebService release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Error during connection: %#", [error description]);
}
Use a JSON parsing framework: http://code.google.com/p/json-framework/

how to parse the json responseString from webservice

I am connecting to webservices using json.
I connected using nsurl connection. I got json response string ... i need to parse it.
When i try to parse it i am getting following error:
dyld: Symbol not found: _CFXMLNodeGetInfoPtr
here is relevant source code:
// Using NSURLConnection
- (void)viewDidLoad
{
[super viewDidLoad];
dataWebService = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse"]]retain];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(#"Response: %#",responseString);
[responseString release];
[dataWebService release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"Error during connection: %#", [error description]);
}
// Parse JSON
- (void)requestCompleted:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSDictionary *dictionary = [responseString JSONValue];
NSDictionary *dictionaryReturn = (NSDictionary*) [dictionary objectForKey:#"request"];
NSString *total = (NSString*) [dictionaryReturn objectForKey:#"totalResults"];
NSLog(#"totalResults: %#", total);
int count = [[dictionaryReturn objectForKey:#"count"] intValue];
NSLog(#"count: %d", count);
}
The JSON response string is not a valid one, that's the reason your are getting the error.
Use the following link to check the if the JSON string, sent as response from Web services is valid one or not
JSONLint