2014-01-12 88 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