1. String conversion to URL string nsstring classification
#import <Foundation/Foundation.h>@interface nsstring (urlencoding)-(NSString * ) urlencodedstring; -(NSString *) urldecodedstring; @end
#import "t20140628013418nsstring+urlencoding.h"@implementationnsstring (urlencoding)-(NSString *) urlencodedstring{NSString*result = (NSString *) Cfbridgingrelease (Cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (Cfstringref) Self, NULL, Cfstr ("!*();+$,%#[] ") , kCFStringEncodingUTF8)); returnresult;}-(nsstring*) urldecodedstring{NSString*result = (NSString *) Cfbridgingrelease (cfurlcreatestringbyreplacingpercentescapesusingencoding (Kcfallocatordefault, (Cfstringref) Self, Cfstr ("") , kCFStringEncodingUTF8)); returnresult;}@end
2. Error message NSNumber classification
#import <Foundation/Foundation.h>@interface nsnumber (Message)-(NSString *) errormessage; @end
#import "t20140628013418nsnumber+message.h"@implementationNSNumber (Message)-(NSString *) errormessage{/*-7 No data. *-No input for the 6th period. *-5 content not entered. *-4 ID not entered. *-3 failed to access the data. *-2 Your account can insert up to 10 data. *-1 user does not exist. * 0 Query Success * 1 modification succeeded*/NSString*errorstr =@""; Switch([self integervalue]) { Case-7: Errorstr=@"no data. "; Break; Case-6: Errorstr=@"The date is not entered. "; Break; Case-5: Errorstr=@"The content is not entered. "; Break; Case-4: Errorstr=@"The ID is not entered. "; Break; Case-3: Errorstr=@"data access failed. "; Break; Case-2: Errorstr=@"you can insert up to 10 data in your account. "; Break; Case-1: Errorstr=@"user does not exist"; default: Break; } returnerrorstr;}@end
3. Get the URL object
NSString *strurl = [[NSString alloc] Initwithformat:@ "http://127.0.0.1:8080/xxx/test01.html" ]; *url = [Nsurl urlwithstring:[strurl urlencodedstring]];
4. Synchronous GET Request
Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url]; *data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
5,-(Nsmutablearray *) findAll
-(Nsmutablearray *) findall{NSString*strurl = [[NSString alloc] Initwithformat:@"http://127.0.0.1:8080/kujizu/test01.html"]; Nsurl*url =[Nsurl Urlwithstring:[strurl urlencodedstring]; Nsurlrequest*request =[[Nsurlrequest alloc] initwithurl:url]; NSData*data =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; if(Data = =Nil) {Self.listdata=[[Nsmutablearray alloc] init]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"error Message"Message@"no data. " Delegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil]; [Alertview show]; }Else{nsdictionary*resdict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; NSNumber*resultcodeobj = [Resdict objectforkey:@"ResultCode"]; if([Resultcodeobj IntegerValue] >=0) {Self.listdata= [Resdict Objectforkey:@"Record"]; } Else{nsstring*errorstr =[Resultcodeobj errormessage]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"error Message"Message:errorstrDelegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil]; [Alertview show]; } } returnSelf.listdata;}