2

私はobjective-Cにかなり慣れていないので、ある時点で打たれました.XMLリクエストをPOSTしてサービスURLを保存し、そこから応答を取得する必要があります.NSMutableRequestを使用していますが、応答が表示されませんでした.私は間違っている...

リクエスト XML は次のようになります。

<GetUserRequest xmlns="http://schemas.datacontract.org/2004/07/DModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ID>456789</ID>
<UserEmail>John@yahoo.com</UserEmail>
</GetUserRequest>

My url is like this : http://192.158.0.104:8732/IServices/GetXml

私のコードは次のようになります:

NSString *urlString1=[NSString stringWithFormat:@"http://192.158.0.104:8732/IServices/GetXml"];
NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:urlString1]];
[request setHTTPMethod:@"POST"];
NSString *contentType=[NSString stringWithFormat:@"application/xml"];
[request addValue:contentType  forHTTPHeaderField:@"Content-type"];

NSMutableData *postBody=[NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<GetUserRequest xmlns=\"http://schemas.datacontract.org/2004/07/DModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<ID>456789</ID>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<UserEmail>John@yahoo.com</UserEmail>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"/GetUserRequest>"]dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSHTTPURLResponse *urlResponse=nil;
NSError *error=[[NSError alloc]init];
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *rss=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding ];
NSlog(@"Response Code:%d",[urlResponse statusCode]);
if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
{
    NSLog(@"Response:%@",rss);
}

しかし、アクティブなコンソールには何も表示されませんでした...

任意の助けをいただければ幸いです...

4

1 に答える 1

0

そのコードは私には貧弱に見え、十分なエラーチェックが含まれていません (エラーが発生したときに[NSURLConnection sendSynchronousRequest]戻ることができnilます)。末尾を次のように変更します。

NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                                             returningResponse:&urlResponse 
                                                         error:&error];
if (responseData != nil)
{
    NSString *rss = [[NSString alloc] initWithData:responseData 
                                          encoding:NSUTF8StringEncoding ];
    NSlog(@"Response Code:%d",[urlResponse statusCode]);
    if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
    {
        NSLog(@"Response:%@",rss);
    } 
}
else
{
    NSLog(@"Failed to send request: %@", [error localizedDescription]);
}
于 2012-10-16T11:10:00.233 に答える