2011-11-18 240 views
0

我正在使用JSON字符串作爲對服務器的請求,並以JSON格式獲取響應。我已經使用這個代碼發佈的數據,如何在iPhone中的Post方法中發送JSON請求?

NSString *requestString = [NSString stringWithFormat:@"{\"id\":\"1\"}}"]; 
NSLog(@"the request string is %@", requestString); 
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http//www.aaaa.com"]]; 
[request setHTTPMethod: @"POST"]; 
[request setHTTPBody: requestData]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[theConnection start]; 

if(theConnection){    
    receiveData = [[NSMutableData data] retain]; 
} 

在服務器端,他們使用了PHP核心與POST方法,每當我發出請求到服務器,我沒有得到的數據和JSON請求文件沒有按沒有達到服務器。所以請幫助我。

但是,上述代碼在另一個項目中完全正常工作,並且他們在服務器端使用了SOAP。所以請建議我,在服務器端或iPhone端,我可以在哪裏更改代碼以實現此目的。

謝謝!

+0

指定的URL看起來錯了,@「HTTP:aaaa.com 「,但我假設你在你的實際代碼中有」http://「。除此之外,我沒有看到任何明顯的錯誤。你網站服務器的錯誤是否有任何線索? – Snips

+0

@ Snips,我已經使用了正確的URL格式,它不起作用和其他線索?。 – Pugal

+0

它仍然不是一個合適的URL。它必須是「@」http://www.aaaa.com「' – Ilanchezhian

回答

1

除了評論,

請添加以下標題也。

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

而且,在這條線,

NSString *requestString = [NSString stringWithFormat:@"{\"id\":\"1\"}}"]; 

它有2個接近大括號。請檢查一下。

更新:

你有沒有實現的NSURLConnectionDelegate方法?

請參閱link

+0

感謝您的答案。我已經使用過這個標題,但它不起作用。 – Pugal

+1

我已經更新了我的答案。 PL。查 – Ilanchezhian

0

喜請下面的行添加到您的代碼.....可能是它會幫助你

[要求的setValue:@ 「應用程序/ JSON」 forHTTPHeaderField:@ 「接受」];

0

使用此代碼

  • 創建之後的參數與有需要的值。
  • 在你的.h文件中實現NSURLConnectionDelegate委託。

     url=[NSURL URLWithString:@"#####called url ####### change this according your need ####"]; 
         post = [NSString stringWithFormat:@"name=%@&tags=%@&location=%@&location_lat=%f&location_lng=%f&pluses=%@&minuses=%@&image=%@&creator=%@",self.txtName.text,tags,self.txtLocation.text,center.latitude,center.longitude,plusValu,minusValu,imgUrlStr,deviceId]; 
    
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    
    
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; 
    
        [request setHTTPMethod:@"POST"]; 
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
        [request setHTTPBody:postData]; 
        NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; 
    
        if(connection){ 
         resData = [NSMutableData data]; 
    
        } 
    

    實現委託方法,並從服務器

    (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
        { 
        NSLog(@"didReceiveResponse %s##### response %@",__FUNCTION__,response); 
    //[resData setLength:0]; 
        } 
    
    (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
        { 
        NSLog(@"didReceiveData %@",[[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] JSONValue]); 
    
        } 
    
        (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
         { 
        //NSLog(@"didFailWithError %s and Error %@",__FUNCTION__,[error userInfo]); 
        //[responseData release]; 
        // [_delegate parserDidFailedWithError:[NSString stringWithFormat:@"Connection failed: %@", [error description]] forAction:_action]; 
        } 
    
    (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
    
        // NSLog(@"connectionDidFinishLoading %s",__FUNCTION__); 
    
    } 
    
0

獲取數據,您需要以下:

NSDictionary *dataDict = @{@"id": @"1"}; 
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:nil]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http//www.aaaa.com"]]; 

[request setHTTPMethod: @"POST"]; 
[request setHTTPBody: requestData]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if (theConnection) {    
    receiveData = [[NSMutableData data] retain]; 
}