Cra*_*aub 3 api json objective-c
我正在开发一个使用API的iphone应用程序.
在' 模拟器 '上它工作正常.在' device '上,api调用所有运行但响应委托都返回EMPTY.
日志:
1) 为设备didReceiveData返回(空,NSData即<20>):
append data <20>
Run Code Online (Sandbox Code Playgroud)
但这适用于模拟器:
append data <dfgdf89fgd.....>
Run Code Online (Sandbox Code Playgroud)
2)的didReceiveResponse予有下面的下面返回装置(设定的NSLog监视NSURLConnection的对象).困惑为什么这不完整:
1 connection <NSURLConnection: 0x147a30>
Run Code Online (Sandbox Code Playgroud)
但这对于模拟器:
1 connection <NSURLConnection: 0x4b76ea0, http://api.site.com/search?id=111>
Run Code Online (Sandbox Code Playgroud)
3)的connectionDidFinishLoading下面是用于模拟器
DONE. Received Bytes: 1400
Run Code Online (Sandbox Code Playgroud)
但是下面的设备:
DONE. Received Bytes: 1
Run Code Online (Sandbox Code Playgroud)
我的代码:
我在API文件中有以下代码:
- (void)fetch: (NSString *)id{
NSMutableData *receivedData = [[NSMutableData alloc]init];//initialised
self.receivedData = receivedData;//set to available globally
NSString *initialUrl = [NSString stringWithFormat: @"http://api.site.com/search?id=%@",id];
NSURL *url = [NSURL URLWithString:initialUrl];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Token token=\"apitoken\"" forHTTPHeaderField:@"Authorization"];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"1 connection %@",connection);
NSLog(@"didReceiveResponse");
[self.receivedData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"2 connection %@",connection);
NSLog(@"append data %@",data);
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//TODO error handling for connection
NSLog(@"Cannot Connect");
if ([_delegate respondsToSelector:@selector(apiFailed:)]) {
[_delegate apiFailed:[error localizedDescription]];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [self.receivedData length]);
NSLog(@"FULL DATA: %@",self.receivedData);
}
Run Code Online (Sandbox Code Playgroud)
该模拟器和设备(我与供应轮廓上和工作iphone)都运行过相同的WiFi(我家的路由器).
API:
API返回少量的JSON,我已经使用不同的JSON集进行了测试,并且设备和模拟器都返回完整的JSON没有问题,我只能让它与THIS API一起使用.
我甚至在不同的位置有相同的JSON(api返回),我可以执行完全相同的请求(带标头和身份验证),它对设备没有任何问题.奇怪的是,连接对象仍然不适合伪造的api服务器.在设备 didReceiveResponse上1 connection <NSURLConnection: 0x1af600>仍然没有与连接url(但在模拟器上运行它有1 connection <NSURLConnection: 0x8193fd0, http://test-server.com:81/api-data.json>).这表明它是设备和模拟器 NSURLCONNECTION对象之间的正常差异.
我觉得可能是认证妨碍了,但API是完全开放的(假设你有令牌)并且没有问题,模拟器加上didFailWithError没有运行..
我在头文件中声明了委托方法.
不知道为什么连接对象和数据对象都会遇到问题.
替代方案:
我在fetch方法中尝试了以下内容,而不是我已经拥有的但是它也适用于模拟器,但同样的事情发生在Device上.
//NSURLCONN and NSDATA in response still return empty???
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://api.site.com/search?id=111"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Token token=\"mytoken\"" forHTTPHeaderField:@"Authorization"];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)
我很欣赏这方面的任何想法.
谢谢
用于检测:
connection:didReceiveResponse:.确保你得到200(你应该经常检查状态代码!)url不是nil).Authorization正确使用标题吗?价值Token token="..."似乎很奇怪.| 归档时间: |
|
| 查看次数: |
1569 次 |
| 最近记录: |