2013-06-11 1 views
0

J'ai ce lien:comment appeler webservice dans xcode par la méthode GET?

fonction

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

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

retour chat_log_id

Quelqu'un peut-il s'il vous plaît me expliquer comment appeler webserive par ce get méthode ou donnez-moi la

solution.

ce que je l'ai fait avec mon code est ci-dessous:

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

Comment puis-je gérer cela?

Merci d'avance.

Répondre

0

Je ne suis pas sûr de votre message si vous voulez "poster" ou "obtenir". Toutefois, compte tenu du fait que vous avez défini votre méthode pour poster, et que vous créez quelque chose de nouveau sur votre serveur, je suppose que vous voulez publier.

Si vous souhaitez publier, vous pouvez utiliser ma méthode wrapper pour une demande de publication.

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

} 

Lorsque la chaîne de requête est formaté comme ceci:

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

Cela aussi tirer de nouveau les données de réponse que vous pouvez transformer en une chaîne comme celui-ci.

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

Si vous essayez de saisir les données du serveur au format 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; 

} 
0

Pls changer ur code comme ceci

- (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 *) données {

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

}

u obtenir la chaîne de réponse JSON comme résultat si u a frappé cette URL. Si vous connaissez l'analyse json, vous pouvez obtenir la valeur en fonction de la clé.

voir le lien: How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

Questions connexes