如何通过GET方法调用xcode中的webservice?

时间:2013-06-11 07:28:24

标签: iphone objective-c web-services getmethod

我有这个链接:

function new_message($ chat_id,$ user_id,$ message,$ recipient_ids)

http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7

返回chat_log_id

任何人都可以通过这个get方法向我解释如何调用webserive或者给我

解决方案。

我对我的代码的处理如下:

-(void)newMessage{

if ([self connectedToWiFi]){                
    NSString *urlString = [NSString stringWithFormat:@"www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/1,1,2"];

    NSLog(@"urlString is %@", urlString);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSURL *requestURL = [NSURL URLWithString:urlString];

    [request setURL:requestURL];
    [request setHTTPMethod:@"POST"];


    [NSURLConnection sendAsynchronousRequest:request                                  queue:[NSOperationQueue mainQueue]

                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSLog(@"ERROR = %@",error.localizedDescription);

                           if(error.localizedDescription == NULL)
                           {

                               NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"response >>>>>>>>> succ %@",returnString);
                               [delegate ConnectionDidFinishLoading:returnString : @"newMessage"];
                           }
                           else
                           {
                               NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"response >>>>>>>>> fail %@",returnString);
                               [delegate ConnectiondidFailWithError:returnString : @"newMessage"];
                           }                               
                       }];        
        }   
}

我该如何处理?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我不确定你的帖子是否要“发布”或“获取”。但是,根据您将方法设置为发布以及您在服务器上创建新内容的事实来衡量,我假设您要发布。

如果你想发帖,你可以使用我的包装方法来发帖请求。

+ (NSData *) myPostRequest: (NSString *) requestString withURL: (NSURL *) url{

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:15.0];

NSData *requestBody = [requestString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[request setHTTPBody:requestBody];

NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

return responseData;

}

请求字符串的格式如下:

 NSString * requestString = [[NSString alloc] initWithFormat:@"username=%@&password=%@", userInfo[@"username"], userInfo[@"password"]];

这也会回放你可以变成这样的字符串的响应数据。

responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

如果您尝试以json格式从服务器获取数据......

+ (NSArray *) myGetRequest: (NSURL *) url{

NSArray *json = [[NSArray alloc] init];

NSData* data = [NSData dataWithContentsOfURL:
                url];
NSError *error;

if (data)
    json = [[NSArray alloc] initWithArray:[NSJSONSerialization
                                           JSONObjectWithData:data
                                           options:kNilOptions
                                           error:&error]];

//NSLog(@"get results: \n %@", json);

return json;

}

答案 1 :(得分:0)

请像这样更改你的代码

- (无效)NewMessage作为{

    NSString *urlString = [NSString stringWithFormat:@"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/27" ];

    NSLog(@"urlString is %@", urlString);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSURL *requestURL = [NSURL URLWithString:urlString];

    [request setURL:requestURL];
    [request setHTTPMethod:@"POST"];


    [NSURLConnection sendAsynchronousRequest:request                                  queue:[NSOperationQueue mainQueue]

                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               NSLog(@"ERROR = %@",error.localizedDescription);

                               if(error.localizedDescription == NULL)
                               {

                                   NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"response >>>>>>>>> succ %@",returnString);
                                   [self parseStringtoJSON:data];
                                   //[delegate ConnectionDidFinishLoading:returnString : @"newMessage"];
                               }
                               else
                               {
                                   NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"response >>>>>>>>> fail %@",returnString);
                                  // [delegate ConnectiondidFailWithError:returnString : @"newMessage"];
                               }                               
                           }];        
}

- (void)parseStringtoJSON:(NSData *)data {

NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"chat id %@",[dict objectForKey:@"chat_log_id"]);

}

如果你点击那个网址,你将得到JSON响应字符串作为结果。如果你熟悉json解析,你可以根据键获得值。

请参阅此链接:How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)