本人不才,自己封装了一个方法,使用本方法 只需传入一个json拼接字符串,一个url字符串,将会获得一个NSDictionary字典数据
#import <Foundation/Foundation.h>
#import "JSON.h"
@interface HttpServer : NSObject
{
NSString *urlString;
NSString *jsonString;
}
-(NSDictionary *)JSONString:(NSString *)jsonString URLString:(NSString *)urlString;
@end
#import "HttpServer.h"
@implementation HttpServer
-(NSDictionary *)JSONString:(NSString *)jsonString URLString:(NSString *)urlString{
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [result JSONValue];
return dictionary;
}
@end