2014-01-12 306 views
-1

您好,我是在新的IOS,我并没有发送任何调用到PHP到现在今天我有下面的代码试图HTTP POST请求

-(void)sendRequest 
{ 

NSString *vali = @"$uppl!3r$"; 

NSString *post = [NSString stringWithFormat:@"key1=%@",vali]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSLog(@"%@",postLength); 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if (theConnection) { 
    webData = [[NSMutableData data] retain]; 
    NSLog(@"%@",webData); 
} 
else 
{ 

} 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     [webData setLength: 0]; 
    } 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[webData appendData:data]; 

} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
[connection release]; 
[webData release]; 

} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:      [webData length] encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",loginStatus); 
//greeting.text = loginStatus; 
[loginStatus release]; 
[connection release]; 
[webData release]; 
} 

它应该返回我的76条记录阵列,但它返回我<>任何人都可以请帮我吗?该网站的服务好了,我需要得到响应的阵列,并显示在我的表视图,请帮我做这个

+0

使用'initWithData:编码:'创建字符串来自数据。 – Wain

+0

我已添加nslog,它会转到didfailwitherror为什么? @Wain – user3089900

+0

我现在加了它,并且我得到了''''和那个ns日志msg让我试试你的曾经@Rob – user3089900

回答

0

一对夫妇的想法:

  1. NSLogwebData,这将始终显示<>(如你的日志立即实例化后话)。我不知道你为什么要记录。

    问题是您是否看到<>connectionDidFinishLoading中的NSLog

  2. 我问,因为你没有在connection:didFailWithError:中记录error,如果失败,你永远不会知道为什么。你真的应该登录connection:didFailWithError:error,所以你知道,如果它失败了,如果是这样,为什么:

    NSLog(@"%s: %@", __FUNCTION__, error); 
    
  3. 在你connection:didReceiveResponse:,你真的应该看看HTTP status code

    if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
        NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; 
    
        if (statusCode != 200) 
         NSLog(@"%s: status code is %d; should be 200", __FUNCTION__, statusCode); 
    } 
    

    如果它是不是200,你真的想知道这件事。

  4. 您在其中一个评论中报告说您看到connectionDidFinishLoading:已被调用,但从未调用过didReceiveData。这意味着(不出所料)没有收到任何数据。所以,你应该:

    • 确认的connection:didReceiveResponse:报告200一个statusCode;和

    • 确认服务器代码工作正常。我可以想象如果你的服务器PHP代码有错误(这是由于服务器在php.ini文件中关闭display_errors而加剧的事实加剧了你的描述)。


顺便说一句,如果可能的话,与key1关联的值可能包含任何保留字符(如RFC 3986 section 2定义),你应该%的转义使用CFURLCreateStringByAddingPercentEscapes字符串。因此:

NSString *post = [NSString stringWithFormat:@"key1=%@", [self percentEscapeString:vali]]; 

其中,每W3C specsapplication/x-www-form-urlencoded,你不仅%的逃避,但也与+字符,从而替换空格:

- (NSString *)percentEscapeString:(NSString *)string 
{ 
    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                       (CFStringRef)string, 
                       (CFStringRef)@" ", 
                       (CFStringRef)@":/[email protected]!$&'()*+,;=", 
                       kCFStringEncodingUTF8)); 
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
} 
+0

首先感谢它我nslog连接失败,它显示我错误,我现在解决它在连接didfinishloading但它没有进入didreceivedata @rob – user3089900

+0

@ user3089900更新的答案(点#4),以反映为什么你可能会看到“完成加载“而不是”接收数据“。 – Rob

+0

我得到的状态码是1003 – user3089900

0

用途:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler 

除非你有一个令人信服的理由不这么做。

实例(可能非工作):

注意创造postData

-(void)sendRequest { 
    NSString *vali = @"$uppl!3r$"; 
    NSString *post = [NSString stringWithFormat:@"key1=%@",vali]; 
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; 
    NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]]; 
    NSLog(@"%@", postLength); 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

           NSLog(@"data: %@", data); 

          }]; 
} 

哦,让生活更轻松,使用ARC

+0

它没有工作人 – user3089900

+0

如果你想知道发生了什么,可以使用Charles Proxy(30免费试用版)等网络分析器,并查看发送和接收的内容。这真的很简单。 – zaph