【问题标题】:Post on website works on simulator but not on device?网站上的帖子可以在模拟器上工作,但不能在设备上工作?
【发布时间】:2012-09-11 04:30:20
【问题描述】:

我想在我的应用程序中使用网络服务并在网站上上传帖子,起初我使用 ASIFormRequest API,它在模拟器上工作但在设备上不起作用,我在这里问了一个问题,有人告诉我改变API 和使用 RestKit (Here is the link for my previous question)

现在我正在使用 RestKit API,我遇到了同样的问题,我无法通过设备在网站上发布,但是当我使用模拟器时它可以工作。虽然它不允许我发帖,但我可以通过模拟器和设备登录。

这是我的 POST 代码:

- (IBAction)addLinkPressed:(UIButton *)sender {
[RKClient clientWithBaseURLString:@"http://MyWebsite.com"];
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            self.linkField.text, @"url",
                            self.linkTitleField.text, @"title",
                            self.linkSummaryField.text, @"summary",
                            nil];

    [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];

}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //Do whatever here to handle authentication failures
    }
    RKLogError(@"Hit error: %@", error);
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    if ([request isGET]) {
        // Handling GET /foo.xml

        if ([response isOK]) {
            // Success! Let's take a look at the data
            NSLog(@"Retrieved XML: %@", [response bodyAsString]);
        }

    } else if ([request isPOST]) {

        // Handling POST /other.json
        if ([response isJSON]) {
            NSLog(@"Got a JSON response back from our POST!");
        }

    } else if ([request isDELETE]) {

        // Handling DELETE /missing_resource.txt
        if ([response isNotFound]) {
            NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
        }
    }

    NSLog(@"HTTP status code:     %d", response.statusCode);
    NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
    NSLog(@"Header fields: %@", response.allHeaderFields);
    NSLog(@"Body: %@", response.bodyAsString);
}

这是我收到的 RestKit API 错误:

    2012-09-11 22:33:01.053 (NameOfMyApp)[16271:707] I restkit.support:RKCache.m:189 Invalidating cache at path: /var/mobile/Applications/897B1DEA-1767-4F5C-AC8F-1809075C5CA9/Library/Caches/RKClientRequestCache-(Mywebsite).com/SessionStore
    2012-09-11 22:33:01.057 (NameOfMyApp)[16271:707] I restkit.network.reachability:RKReachabilityObserver.m:123 Reachability observer initialized with IP address: 0.0.0.0.
    2012-09-11 22:33:01.075 (NameOfMyApp)[16271:707] I restkit.network.reachability:RKReachabilityObserver.m:391 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x10066330 host=0.0.0.0
2012-09-11 22:33:01.075 (NameOfMyApp)[16271:707] I restkit.network.reachability:RKReachabilityObserver.m:391 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x10066330 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-11 22:33:01.325 (NameOfMyApp)[16271:707] I restkit.network:RKRequest.m:689 Status Code: 200
2012-09-11 22:33:01.328 (NameOfMyApp)[16271:707] HTTP status code:     200
2012-09-11 22:33:01.332 (NameOfMyApp)[16271:707] HTTP status message:  no error
2012-09-11 22:33:01.338 (NameOfMyApp)[16271:707] Header fields: {
    "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 4679;
    "Content-Type" = "text/html";
    Date = "Wed, 12 Sep 2012 03:33:02 GMT";
    Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Pragma = "no-cache";
    Server = Apache;
    Vary = "Accept-Encoding";
}
2012-09-11 22:33:01.345 (NameOfMyApp)[16271:707] Body: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

在这些代码之后,它显示了我用于 POST 的地址的 HTML 代码(http://MyWebsite.com/send_link.php

任何想法将不胜感激,我真的需要帮助:(

【问题讨论】:

  • 你有没有实现RKRequestDelegate的didFailLoadWithError方法,并且在这个方法中打印错误的描述,如果你提供你得到的确切错误会更好,如果可能的话也提供一些代码。
  • @prasad 感谢您的评论,我编辑了我的问题并添加了收到的错误,如果您能帮助我,我将不胜感激。
  • 您能否展示如何进行 POST 调用以及如何使用 Web 服务的基本 URL 设置 RestKit?
  • @danielbeard 我编辑了我的问题并添加了我用于 POST 的代码,感谢您的评论。
  • 发生了您意想不到的事情?它给出的状态码是200 OK

标签: ios objective-c ios-simulator


【解决方案1】:

我找到了答案,在模拟器中不需要将 http:// 放在 url 地址的开头,但在设备上,如果我不输入 http:// 我使用他们的网络服务的网站不允许我发布。是不是很奇怪! 谢谢你们的cmets。 另一件事是,当我关闭应用程序并重新打开它时,我必须登录,知道如何使用户名和密码持久化吗?

【讨论】:

  • 看看钥匙扣。 SSKeycahin 让这很容易,但这确实属于一个单独的问题。
  • @danielbeard 感谢您提供线索。
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2023-03-23
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
相关资源
最近更新 更多