2010-10-19 10 views
1

Comment faire une demande HTTP Post en utilisant JSON. J'ai essayé toutes les options disponibles sur Internet. Mais je n'ai pas pu obtenir les données. Alors s'il vous plaît poster le code entier pour faire une demande.Http Post Request

+0

Ce n'est pas un "code pour moi gratuitement" site. La façon dont cela fonctionne est, vous postez votre code et les gens vous aident à le faire fonctionner. Cela dit, vous devriez google ASIHTTPRequest. C'est une bibliothèque de client HTTP très robuste mais relativement très simple, et la page "comment l'utiliser" a beaucoup d'exemples de code. –

Répondre

3

Voici un exemple de base de NSURLConnection POST -ING JSON à une URL.

- (void)performRequest { 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://someplace.com/"]]; 
    [request setValue:@"Some Value" forHTTPHeaderField:@"Some-Header"]; 
    [request setHTTPBody:@"{\"add_json\":\"here\"}"]; 
    [request setHTTPMethod:@"POST"]; 
    [NSURLConnection connectionWithRequest:[request autorelease] delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    // Fail.. 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // Request performed. 
} 
4

Vous pouvez utiliser ci-dessous le code:


-(void)performRequest{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [jsonMessage length]]; 
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue: jsonAction forHTTPHeaderField:@"JSONAction"]; 
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody: [jsonMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 
    [pool release]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    [webData setLength: 0]; 
    self.resultArray = [[NSMutableArray alloc] init]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    [webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    NSLog(@"ERROR with theConenction"); 
    NSDictionary *errorDic = [NSDictionary dictionaryWithObject:error forKey:@"error"]; 
    [self.resultArray addObject:errorDic]; 
    [connection release]; 
    [webData setLength:0]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", theXML); 
    [theXML release]; 
    if([webData length] > 0){ 
     parser = [[NSXMLParser alloc] initWithData:webData]; 
     [parser setDelegate:self]; 
     [parser parse]; 
    } 
}