1

我想在我的应用程序中使用网络服务并在网站上上传帖子,起初我使用 ASIFormRequest API,它在模拟器上工作但在设备上不起作用,我在这里问了一个问题,有人告诉我更改 API 并使用RestKit(这是我上一个问题的链接

现在我正在使用 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 ( http://MyWebsite.com/send_link.php )的地址的 HTML 代码

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

4

1 回答 1

0

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

于 2012-09-12T17:23:18.863 回答