IOS. Accessing the Web Service. Synchronous GET Request method

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.