OGeek|极客世界-中国程序员成长平台

标题: iphone - ASIHTTPRequest 在模拟器上工作,但在 iphone 上工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:05
标题: iphone - ASIHTTPRequest 在模拟器上工作,但在 iphone 上工作

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

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

所以我将它设置为 asihttprequest 方法,与其他不缓存缓存的方法相同。

我从主视图中选择了一个表格单元格来加载第二个 View 并将一堆信息解析到表格 View 中。用户然后选择一个值,该值被传回主视图并显示。

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

这里有一张流程图,可以解释我正在努力做得更好 enter image description here

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

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

    - (void) setManufactureSearchFieldsNSArray *)arrayValues withIndexPathNSIndexPath *)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)setRequestStringNSString *)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)requestFinishedASIHTTPRequest *)request
{   
        responseString = [request responseString]; //Pass requested text from server over to NSString 
        capturedResponseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 

        [self startTheParsingProcess:capturedResponseData];
}

这是在 iphone 上进行测试时触发的唯一其他委托(delegate),向我发送警报说连接已超时。

- (void)requestFailedASIHTTPRequest *)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)..也许我应该在重新加载表格或其他东西之前设置值...我不确定但希望有人可以帮助我解决这个问题并发现我的问题看不见。



Best Answer-推荐答案


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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4