iphone - ASIHTTPRequest 在模拟器上工作,但在 iPhone 上不工作

标签 iphone ios protocols asihttprequest

嘿,到目前为止,我已经使用 ASIHTTPRequest 做了很多工作,并在我的 ios 应用程序中多次使用它,但是在我的应用程序的一个区域中,我添加了另一个 asihttprequest 方法,但它无法正常工作。

首先必须说的是,我试图分散下载和解析数据的负载。如果我在 subview 上执行此操作,则需要 2-3 秒的时间来浏览第二个 xml工作表并获取所有相关值。如果我在这个主视图上执行此操作,人们没有看到任何加载等,那么当返回到 subview 时,它应该看起来几乎是即时的。我不知道这有多正确,但我认为这样做可以让应用程序感觉更敏捷。

因此,我将其设置为 asihttprequest 方法,其设置与其他不使用缓存的方法相同。

发生的情况是我从主视图中选择一个表格单元格,该单元格加载第二个 View 并将一堆信息解析到表格 View 中。然后,用户选择一个值,该值将传递回主视图并显示。

然后,我解析另一批 xml 数据,根据第二个 xml 表中的所有内容检查所选 valueID。这样,当用户选择第二个单元格时,我将刚刚解析的所有数据传递到第二个 View ,使其看起来加载速度更快。

这是一个流程图,将解释我试图做得更好一点 enter image description here

这就是解析器代码在主视图中的样子,该代码在模拟器中工作,但在 iPhone 上不起作用。

这是我从 subview 调用的协议(protocol),并将需要传递的所有值传递给主视图 ASIHTTPRequest 并触发请求。

    - (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
    {
        manufactureSearchObjectString = [[arrayValues valueForKey:@"MANUFACTURER"] objectAtIndex:0];
        manufactureIdString = [[arrayValues valueForKey:@"MANUID"] objectAtIndex:0]; //Restricts Models dataset
        manufactureResultIndexPath = myIndexPath;
        [self.tableView reloadData]; //reloads the tabels so you can see the value in the tableViewCell.
        //need some sort of if statment here so that if the back button is pressed modelSearchObjectString is not changed..

        if (oldManufactureSearchObjectString != manufactureSearchObjectString) {
            modelResultIndexPath = NULL;
            modelSearchObjectString = @"empty";
            oldManufactureSearchObjectString = [[NSString alloc] initWithFormat:manufactureSearchObjectString];
        }
//These two lines below are what execute ASIHTTPRequest and set up my parser etc
        dataSetToParse = @"ICMod"; // This sets the if statment inside parserDidEndDocument
        [self setRequestString:@"ICMod.xml"]; //Sets the urlstring for XML inside setRequestString

    }

然后这将触发 ASIHTTPRequest 委托(delegate)方法。

- (IBAction)setRequestString:(NSString *)string
{
    //Set database address
    //NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8888/codeData/"]; // imac development
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8888/codeData/"]; // iphone development

    //PHP file name is being set from the parent view
    [databaseURL appendString:string];

    NSLog(@"%@", databaseURL);

    //call ASIHTTP delegates (Used to connect to database)
    NSURL *url = [NSURL URLWithString:databaseURL];

    //This sets up all other request
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    [request setDelegate:self];
    [request startAsynchronous];

}

当我在 iPhone 上测试时通过断点调试运行此程序时,这就是应用程序崩溃的地方..但在模拟器上它没有问题。

在 iPhone 上测试时,下一个方法永远不会被调用,但在模拟器上运行良好。

- (void)requestFinished:(ASIHTTPRequest *)request
{   
        responseString = [request responseString]; //Pass requested text from server over to NSString 
        capturedResponseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 

        [self startTheParsingProcess:capturedResponseData];
}

这是在 iPhone 上测试时唯一被触发的其他委托(delegate),它向我发送一条警报,表示连接已超时。

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A connection failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [errorAlert show];
}

我认为您不需要查看所有解析器委托(delegate),因为我认为它们不是问题,因为这就是应用程序崩溃的地方......

这是打印到日志中的内容...

2011-11-29 14:38:08.348 code[1641:707] http://000.000.000.000:0000/codeData/second.xml
2011-11-29 14:38:18.470 code[1641:707] Error Domain=ASIHTTPRequestErrorDomain Code=2 "The request timed out" UserInfo=0x1e83a0 {NSLocalizedDescription=The request timed out}

如果您需要更多我的代码,请告诉我..但是我在这里有点不知所措,就像我说的那样,除了我初始化它之外,我对其他 View 执行此 ASIHTTPRequest 的方式没有什么区别我从第二个 View 中设置的协议(protocol)..也许我应该在重新加载表或其他东西之前设置值...我不确定,但希望有人可以帮助我解决这个问题并发现我的问题看不到。

最佳答案

您可以在 iPhone 上使用 Safari 查看“http://127.0.0.1:8888/codeData/”吗?无论 iPhone 连接到什么网络,服务器都可能无法使用。

如果您的 iMac 使用 DCHP,则自您最初设置该值以来,该地址可能已更改。

关于iphone - ASIHTTPRequest 在模拟器上工作,但在 iPhone 上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8304818/

相关文章:

ios - Xcode 5 与 iOS 6 SDK : 'UIAccelerometer' is unavailable: not available on OS X

iphone - UIImageView 不同区域的 UIGesture 识别

iOS 可设计按钮背景颜色不起作用

iOS7 UITableView scrollsToTop 不起作用

http - 如何知道何时停止 HTTP 代理连接中的数据隧道

python - 签名中带有 TypeVar 的协议(protocol)的实现者不能使用自己的类型

iphone - Objective-C : How to grab a number after a sub string from string

iphone - 使用 genstrings 本地化 Xcode 源文件?

ios - 通过 popToViewController 在 swift 中传递变量值

ios - 设置 self.delegate = self 是不是糟糕的设计