How to make Http Post Request using JSON .I tried every option which is available on Internet .but couldnot get the Data .So please post entire code to make a request
A:
This isn't a "code it for me for free" site. The way this works is, you post your code and people help you make it work.
That said, you should google ASIHTTPRequest. It's a very robust but relatively very simple HTTP client library, and the "how to use it" page has plenty of sample code.
Dan Ray
2010-10-19 12:11:36
Likewise, this is as much a comment as it is an answer ;)
August Lilleaas
2010-10-19 12:13:25
Why the down vote?
Jordan
2010-10-19 14:13:13
+1
A:
Here's a basic example of NSURLConnection POST-ing JSON to an 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.
}
August Lilleaas
2010-10-19 12:12:31
A:
You can use below 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];
}
}
jfalexvijay
2010-10-19 15:13:26