我想在我的应用程序中使用Web服务并在网站上传帖子,起初我使用的是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)
任何想法怎么能被欣赏,我真的需要帮助:(
答案 0 :(得分:0)
我找到了答案,在模拟器中它不需要将http://放在url地址的开头,但是在设备上,如果我不输入http://我使用他们的web服务的网站不允许我发帖。这不奇怪! 谢谢你们的评论。 还有一件事是,当我关闭应用程序并重新打开它时,我必须登录,任何想法如何使用户名和密码持久性?