2011-09-21 86 views
2

我有用post方法xmlparsing的問題。xml使用Post方法解析+ iphone

API URL : http://XXX.XXX.X.XX/api/user.php 
Function Name : getUserList 
Sample XML : 
<root> 
    <data> 
     <id>0</id> 
     <search></search> 
    </data> 
</root> 

我現在用: -

// setting up the URL to post to 
NSString *urlString = @"http://XXX.XXX.X.XX/api/user.php"; 

// setting up the request object now 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"];   

但我要如何在這後HTMLbody部分..

指,我應該如何把功能名稱和示例XML中代碼

我編輯的問題是: -

NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList"; 

    // setting up the request object now 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 
    NSString * str = @"<root><data><id>0</id><search>a</search></data></root>"; 
    NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str]; 
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 





    // now lets make the connection to the web 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(@"Responce==>%@",returnString); 

但我越來越黑responce.Pleaseee幫我.. 是我

NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList"; 

而且

NSString * str = @"0a"; NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str]; [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

這部分代碼是正確的?

回答

1

您可以使用方法- (void)setHTTPBody:(NSData *)data來設置html body。例如:

[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; 

其中的消息,在你的情況下,xml。

你也需要添加此代碼,以幫助服務器,以確定發送的數據的類型:如果你想設置一些附加標誌您的要求,您可以使用方法- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

[request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

+0

感謝您的快速回復。可以給我這個想法,我應該如何獲得響應數據。我使用過這個。請指導我是否正確:-NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error :零]; \t NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; –

+1

它看起來不錯,但爲什麼你不想檢查返回的響應代碼? – Nekto

+0

我想檢查性反應的即時得到throigh這是否是正確的或不 –

0

你所做的是完美的,請檢查服務器端是否通過發送相同的請求來響應。