简体   繁体   中英

how to call webservice in xcode by GET Method?

I have this link :

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

return chat_log_id

Can anyone please explain me how to call webserive by this get method or give me the

solution .

what i did with my code is below :

-(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"];
                           }                               
                       }];        
        }   
}

how can i handle this ?

Thanks in advance .

I am not sure from your post whether or not you want to "post" or "get." However, gauging from the fact that you set your method to post, and that you are creating something new on your server, I am assuming you want to post.

If you want to post you can use my wrapper method for a post request.

+ (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;

}

Where request string is formatted like this:

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

This will also shoot back the response data which you can turn into a string like this.

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

If you are trying to grab data from the server in json format...

+ (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;

}

Pls change ur code like this

-(void)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"]);

}

u will get the JSON response string as result if u hit that url. If ur familiar with json parsing, u can get the value based on key.

see this link: How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM