准备GET/POST请求以在iPhone上获取数据
我正在尝试为搜索词“cancer”获取JSON格式的数据 但是我不知道怎么称呼这个网站,我尝试了一些方法,但都不起作用,有人能帮我吗 下面是我应该调用的API 单击以下URL将在浏览器中获得所需的数据。 apiKey=2be58f97 以下是我正在使用的代码: NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init]; NSURL*requestURL=[NSURL URLWithString:@''; [请求设置URL:requestURL]; [请求设置HttpMethod:@“获取”]准备GET/POST请求以在iPhone上获取数据,iphone,ios,nsurlconnection,asihttprequest,Iphone,Ios,Nsurlconnection,Asihttprequest,我正在尝试为搜索词“cancer”获取JSON格式的数据 但是我不知道怎么称呼这个网站,我尝试了一些方法,但都不起作用,有人能帮我吗 下面是我应该调用的API 单击以下URL将在浏览器中获得所需的数据。 apiKey=2be58f97 以下是我正在使用的代码: NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init]; NSURL*requestURL=[NSURL URLWithString:@''; [请求设置URL:request
在代码中设置多部分/表单数据请求。虽然这一成就是值得称赞的,但这与您如何与给定web服务的API交谈无关 事实上,它更简单: 正如您可以从该站点的文档中检索到的那样,“查询参数”作为查询字符串进入URL:“q=cancer”。然后,只需将内容类型头指定为“application/json”,它就可以工作了 通常,URL查询参数将在URL前面加上“?”,然后加上包含查询字符串的“非层次数据”,然后再加上可选的“#” “非层次数据”的含义并没有明确规定,但在几乎所有情况下,web服务都需要一个查询字符串作为键/值对的列表,其键和值用“=”分隔,对用“&”分隔:
param1=value1¶m2=value2
此外,为了消除查询字符串的歧义,例如,当值或键本身包含“特殊字符”(如空格、非ASCII字符、符号或等号等)时,查询字符串必须在附加到URL并发送到服务器之前进行正确的“URL编码”
有关构造URL的详细信息,请参阅相应的RFC。但是,wiki以更简洁的形式提供了一个可理解的查询字符串定义:
有关如何使用方便的方法或函数“URL编码”查询字符串的更多信息,请阅读NSString
文档,stringByAddingPercentEscapesUsingEncoding:
和核心基础:CFURLCreateStringByAddingPercentEscapes
:
第三方库可能会使这更方便,但是您应该了解API的含义,以及您必须如何自己构造URL、HTTP头和查询字符串。展示一些您尝试过的代码。
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"q\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",searchText] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"ERROR = %@",error.localizedDescription);
if(error.localizedDescription == NULL)
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response >>>>>>>>> %@",returnString);
}
else
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response >>>>>>>>> %@",returnString);
}
}];
-(AFHTTPClient *) getHttpClient{
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBASEURL]];
httpClient.parameterEncoding = AFJSONParameterEncoding;
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
return httpClient;
}
//This is how you should call
-(void) callAPI{
AFHTTPClient *httpClient = [self getHttpClient];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:method parameters:queryStrDictionary];// querystringDictionary contains value of all q=? stuff
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//You have Response here do anything you want.
[self processResponseWith:JSON having:successBlock andFailuerBlock:failureBlock];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
//Your request failed check the error for detail
failureBlock(error);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init] ;
[queue addOperation:operation];
}