3

我正在开发一个使用 API 的 iphone 应用程序。

在“模拟器”上它工作正常。在“设备”上,api 调用全部运行,但响应委托全部返回 EMPTY。

LOG:
1)在下面为 设备didReceiveData返回(空NSData,即 <20>):

append data <20>

但这对于模拟器

append data <dfgdf89fgd.....>


2)didReceiveResponse我有下面的设备返回(设置NSLog 以监视 NSURLConnection 对象)。困惑为什么这不完整:

1 connection <NSURLConnection: 0x147a30>

但这对于模拟器

1 connection <NSURLConnection: 0x4b76ea0, http://api.site.com/search?id=111>


3)下面connectionDidFinishLoading模拟器

DONE. Received Bytes: 1400

但下面的设备

DONE. Received Bytes: 1


我的代码:
我在 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);
}

模拟器设备(我的iphone 配置了配置文件并正在工作)都在同一个 Wifi(我的家庭路由器)上运行。


API:
API 返回少量 JSON,我已经使用不同的 JSON 集进行了测试,设备和模拟器都返回完整的 JSON 没问题,我只能让它与这个 API 一起工作。
我什至在不同的位置有相同的 JSON(api 返回),我可以执行完全相同的请求(带有标头和身份验证),它对device没有问题。奇怪的是,虽然连接对象仍然不适合伪造的 api 服务器。在设备 didReceiveResponse1 connection <NSURLConnection: 0x1af600>仍然没有连接 url(但在模拟器上运行时它有1 connection <NSURLConnection: 0x8193fd0, http://test-server.com:81/api-data.json>)。这表明它是设备模拟器 NSURLCONNECTION对象之间的正常差异。
我觉得这可能是身份验证的障碍,但 API 是完全开放的(假设你有令牌)并且在模拟器中没有问题加上didFailWithError没有运行..

我已经在头文件中声明了委托方法。
不知道为什么连接对象和数据对象都会出现问题。


替代方案:
我在 fetch 方法中尝试了下面的方法,而不是我已经尝试过的方法,但它也适用于Simulator但同样的事情发生在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];

我将不胜感激有关此的任何想法。

谢谢

4

1 回答 1

5

用于检测:

  1. 从请求中删除缓存(它可以做一些非常有趣的事情)。
  2. 打印出 HTTP 状态码connection:didReceiveResponse:。确保你得到 200(你应该经常检查状态码!)
  3. 在其他一些 REST 客户端中测试连接(大多数浏览器的插件都存在)。
  4. 确保 API 不是“用户代理”敏感的。
  5. 如果使用“https”协议,请确保证书由设备已知的根权限签署。
  6. 使用调试器检查一切是否正常工作(例如urlis not nil,在创建请求时)。
  7. 你确定你Authorization正确使用了标题吗?价值Token token="..."似乎很奇怪。
于 2013-04-21T16:17:31.407 回答