Thursday 1 October 2015

how to use make parsing using json in ios/Iphone

=>Here  Use to Html Request Url  in your project then Import Some files:
HTMLManeger.h
---------------------------------------------------------------------------------------
//
//  HttpManager.h


#import <Foundation/Foundation.h>

@protocol HTTPManagerDelegate;

@interface HttpManager : NSObject {
    NSMutableURLRequest *theRequest;
    __weak id<HTTPManagerDelegate> delegate;
    NSURLConnection *connection;
}

@property (weak)id<HTTPManagerDelegate> delegate;
@property (nonatomic, retain)NSURLConnection *connection;
@property (nonatomic, retain)NSMutableURLRequest *theRequest;

-(void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent;
-(void)asynchronousRequestServerWithDataPost:(NSString *)urlAddress PostData:(NSData *)postContent;
-(void)asynchronousRequestServerWithMultipartPost:(NSString *)urlAddress PostData:(NSData *)postContent Boundry:(NSString *)contentBoundry;
-(void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent RequestType:(int) requestType;

-(void)asynchronousRequestServerWithImagePost:(NSString *)urlAddress PostData:(NSData *)imageContent PostDict:(NSDictionary *)Content;
-(void)asynchronousRequestServerWithDictPost:(NSString *)urlAddress PostData:(NSDictionary *)postContent RequestType:(int) requestType;
- (void)asynchronousRequestServerWithDictDelete:(NSString *)urlAddress PostData:(NSDictionary *)postContent;



-(void)sendSyncRequest;

-(NSString *)encodeStringForURL:(NSString *)originalURL;

@end

@protocol HTTPManagerDelegate

- (void)connection:(HttpManager *)theConnection didFailWithError:(NSError *)error;
- (void)connectionDidFinish:(HttpManager *)theConnection;
- (void)connection:(HttpManager *)connection didReceiveData:(NSData *)data;
- (void)connection:(HttpManager *)aConnection didReceiveResponse:(NSURLResponse*)response;

@end

-----------------------------------------------------------------------------------------------------------------------
then htmlmanger.m
----------------------------------------------------------------------------
//
//  HttpManager.m

#import "HttpManager.h"


@implementation HttpManager

@synthesize delegate;
@synthesize theRequest;
@synthesize connection;

- (void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent {
   
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
   
    if([postContent length] > 0)
    {
        [theRequest setHTTPMethod:@"POST"];

    } else {
        [theRequest setHTTPMethod:@"GET"];
    }
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding];
    [theRequest setHTTPBody:theBodyData];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
       
    }else {
        NSLog(@"Error");
    }
    [NSURLConnection cancelPreviousPerformRequestsWithTarget:nil];

}

- (void)asynchronousRequestServerWithXMLPost:(NSString *)urlAddress PostData:(NSString *)postContent RequestType:(int) requestType {
   
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];

    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
   
//    if(requestType > 0)
//    {
//        [theRequest setHTTPMethod:@"POST"];
//       
//    } else {
//        [theRequest setHTTPMethod:@"GET"];
//    }
   
    if(requestType == 0)
    {
        [theRequest setHTTPMethod:@"GET"];
    }
    else if(requestType == 1)
    {
        [theRequest setHTTPMethod:@"POST"];
    }
    else if(requestType == 2)
    {
        [theRequest setHTTPMethod:@"PUT"];
    }
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData *theBodyData = [postContent dataUsingEncoding:NSUTF8StringEncoding];
    [theRequest setHTTPBody:theBodyData];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
       
    }else {
       
        NSLog(@"Error");
    }
    [NSURLConnection cancelPreviousPerformRequestsWithTarget:nil];
   
}

- (void)asynchronousRequestServerWithDictPost:(NSString *)urlAddress PostData:(NSDictionary *)postContent RequestType:(int) requestType {
   
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    NSError *error;
    if(requestType == 0)
    {
        [theRequest setHTTPMethod:@"GET"];
    }
    else if(requestType == 1)
    {
        [theRequest setHTTPMethod:@"POST"];
    }
    else if(requestType == 2)
    {
        [theRequest setHTTPMethod:@"PUT"];
    }
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData *theBodyData = [NSJSONSerialization dataWithJSONObject:postContent options:0 error:&error];
    [theRequest setHTTPBody:theBodyData];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
       
    }else {
       
        NSLog(@"Error");
    }
    [NSURLConnection cancelPreviousPerformRequestsWithTarget:nil];
   
}

- (void)asynchronousRequestServerWithDictDelete:(NSString *)urlAddress PostData:(NSDictionary *)postContent{
   
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    NSError *error;
    [theRequest setHTTPMethod:@"DELETE"];
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSData *theBodyData = [NSJSONSerialization dataWithJSONObject:postContent options:0 error:&error];
    [theRequest setHTTPBody:theBodyData];
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
       
    }else {
       
        NSLog(@"Error");
    }
    [NSURLConnection cancelPreviousPerformRequestsWithTarget:nil];
   
}


- (void)asynchronousRequestServerWithImagePost:(NSString *)urlAddress PostData:(NSData *)imageContent PostDict:(NSDictionary *)Content
{
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [theRequest setHTTPBody:imageContent];
   
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
       
    }else {
       
        NSLog(@"Error");
    }
    [NSURLConnection cancelPreviousPerformRequestsWithTarget:nil];
   

}

- (void)asynchronousRequestServerWithDataPost:(NSString *)urlAddress PostData:(NSData *)postContent {

    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f];
    [theRequest setHTTPMethod:@"POST"];
   
   
    if(postContent)
    {
        [theRequest setHTTPBody:postContent];
    }
   
   
    [NSThread detachNewThreadSelector:@selector(sendSyncRequest) toTarget:self withObject:nil];
   
}

- (void)asynchronousRequestServerWithMultipartPost:(NSString *)urlAddress PostData:(NSData *)postContent Boundry:(NSString *)contentBoundry {
    urlAddress = [self encodeStringForURL:urlAddress];
    NSURL *theURL = [NSURL URLWithString:urlAddress];
    theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest addValue:contentBoundry forHTTPHeaderField: @"Content-Type"];
   
    if(postContent)
    {
        [theRequest setHTTPBody:postContent];
    }
   
   
    [NSThread detachNewThreadSelector:@selector(sendSyncRequest) toTarget:self withObject:nil];   
}

- (void)sendSyncRequest {
   
    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self] ;
    if (connection) {
        [connection start];
    }
}

- (NSString *)encodeStringForURL:(NSString *)originalURL {
    NSMutableString *escaped = [[NSMutableString alloc] init] ;
    [escaped appendString:[originalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];      
    return escaped;   
}



-------------------------------------------------------------------------

- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse*)response
{
    [delegate connection:self didReceiveResponse:response];
}

--------------------------------------------------------------------------

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
    [delegate connection:self didReceiveData:data];
}

--------------------------------------------------

-(void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    [delegate connectionDidFinish:self];
}

------------------------------------------------------------------------------
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [delegate connection:self didFailWithError:error];
}


@end