Wednesday, June 13, 2012

How to parse JSON values without tag in iPhone

Here i post how to parse JSON values without using tags...

this is sample json values without tag except main tag.


{
  "Parsing_Json": [
    "[7, Best, hello]",
    "[2, Day, welcome]",
    "[3, News, here]",
    "[6, Test, postings]",
    "[4, Mother, jsonvalues]"
  ]
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[responseData release];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
array = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Parsing_Json"]; 
NSString *str =[NSString stringWithFormat:@"%@",array];

for (int i=0; i<[array count]; i++)
{
NSString *member=[array objectAtIndex:i];
NSArray *array1 =[member componentsSeparatedByString:@","]; 
NSString *n1=[array1 objectAtIndex:0];
NSString *n2=[array1 objectAtIndex:1];
NSString *n3=[array1 objectAtIndex:2];

}



}

In above n1, n2 and n3 string values are parsed from JSON values. This is very easy method to getting JSON values without using tags except head tag.

Thanks...




Saturday, May 5, 2012

SOAP Web Services in iPhone/iPad

Here i posted the code. simply follow the steps.....

Create one button in xib and implemented with name -(IBAction) read_data,
bcz when we press the button then it fired soap webservice.

firstView.h file

@interface firstView : UIViewController {


NSMutableArray *list;
UILabel *soapValue;
}
- (IBAction) read_data;
@end

firstView.m file

@implementation firstView


- (IBAction) read_data
{
 NSString *name=@"50";     // Here name is, we providing value. so u change to ur provide value.

NSString *soapMessage = [NSString stringWithFormat:
@"\n"
"\n"
"\n"
"n"
"%@n"
"n"
"\n"
"\n",name
];

NSLog(@"soap Message= %@",soapMessage);
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];    //Here is the your server URL

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];   

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(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);     //theXML is the ur server XML
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:webData];
[parser setDelegate:self];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
}


-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
{
if([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
list = [[NSMutableArray alloc] init];
}
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
{
if([elementName isEqualToString:@"CelsiusToFahrenheitResult"])
{
list = [[NSMutableArray alloc] init];
}
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
if(string)
{
NSLog(@"String Value= %@",string);    //this string value is the response value splitted from xml
}
}

Thanks 
----------------------

Ravi Kumar K