如何从URL目标c获取文本文件?

时间:2015-06-04 07:38:04

标签: objective-c nsurlconnection nsurlrequest google-translate ios8.1

目前我正在处理翻译应用程序,我从read text file returned by URL提到了翻译文本文件

https://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=" +                     sl +"& tl =" + tl +"& ie = UTF-8& oe = UTF-8& multires = 1& oc = 1& otf = 2& ssel = 0& tsel = 0& sc = 1& q =&#34 ;  。这在android中可以正常工作,但在iOS中它将文本返回为 以下代码我用来获取文件

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=en&tl=ta&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=summer"]
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];

connectionRequest=[[NSURLConnection alloc]initWithRequest:request delegate:self];
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"newStr%@",newStr);

我在做什么?请有人帮我摆脱这个......谢谢

2 个答案:

答案 0 :(得分:0)

我建议使用谷歌的网络服务。可以阅读官方指南。 enter link description here

答案 1 :(得分:0)

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"http://translate.google.com.tw/translate_a/t?client=t&hl=en&sl=en&tl=ta&ie=UTF-8&oe=UTF-8&multires=1&oc=1&otf=2&ssel=0&tsel=0&sc=1&q=summer"]];
[request setHTTPMethod:@"GET"];
[self addHeaders:request];
[NSURLConnection connectionWithRequest:request delegate:self];
[pool release];

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {

    NSString* newStr = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
}