开发者

Iphone dev : how can I get a result from google currency converter?

Sorry for my english ^^ I would know how I can have a result from the google converter tool for currency, available on http://www.google.com/finance/converter?a=1&from=USD&to=GBP for example.

I have already tried something like that :

    -(IBAction)getResponse:(id) sender{

     NSString *myURLString = @"http://www.google.c开发者_如何学Pythonom/finance/converter?a=1&from=USD&to=GBP";

     NSURL *myURL =  [NSURL URLWithString: myURLString];

     NSData *data = [NSData alloc];

     data=[NSData dataWithContentsOfURL:myURL];


     if(data!=nil && data != ""){

          NSString *response = [[[NSString alloc] initWithData:data]]

          [label setText: response];

     }

     else [label setText:@"not working"];
}

But when I click on my button, the label does not have a text (it is like it is empty). Am I doing something wrong ? Is it possible what I want to do ? Thank you very much for your futures answers ! Olivier.


Yeah.. It is possible... See the data that you are getting is an HTML file or can be treated as xml. Just parse that xml file and get the result. There ia lot of things in that file but you have to only extract

<div id=currency_converter_result>1 USD = <span class=bld>0.6118 GBP</span> 

Hey try this URL http://www.google.com/ig/calculator?hl=en&q=1USD=?INR

This URL will return the JSON

{lhs: "1 U.S. dollar",rhs: "44.5097254 Indian rupees",error: "",icc: true}

Just parse it. :)


Try...

NSData *data = [[NSData alloc] init];

Then check you've got something in response

NSLog(@"%@", response);

Better yet, remove the NSData alloc/init

Replace with

 

NSData *data=[NSData dataWithContentsOfURL:myURL];

And, you'll need to parse the return result, because you're not going to get back what you expect. Response will contain HTML page.


You better use an asynchronous request to avoid blocking your app. Easy using ASIHTTPRequest:


- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://www.google.com/finance/converter?a=1&from=USD&to=GBP"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];



   [label setText: responseString];

}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error]; //you can log the error description

   [label setText:@"not working"];

}


NSString *myURLString = @"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";

NSURL *myURL =  [NSURL URLWithString: myURLString];

NSData *data = [NSData alloc];

data=[NSData dataWithContentsOfURL:myURL];


if(data!=nil){

    NSString *response = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];

    NSLog(@"%@",response);
}

Try this. This API is giving HTML responce.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜