IOS Study Notes OC-API-network access-Case 1, 2015-03-24oc-api-
//// WPSuggest. h // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Foundation/Foundation. h> # pragma mark-class feedback @ interface WPSuggest: NSObject # pragma mark-attribute @ property (nonatomic, copy) NSString * _ system; @ property (nonatomic, copy) NSString * _ version; @ property (nonatomic, copy) NSString * _ productType; @ property (nonatomic, copy) NSString * _ Suggest; @ property (nonatomic, copy) NSString * _ concat;-(id) initWithObject :( NSString *) system :( NSString *) version :( NSString *) productType :( NSString *) suggest :( NSString *) concat; @ end // WPSuggest. m // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "WPSuggest. h "@ implementation WPSuggest-(id) initWithObject :( NSString *) system :( NSString *) version :( NSString *) productType :( NSString *) suggest :( NSString *) concat {if (self = [super init]) {self. _ system = system; self. _ version = version; self. _ productType = productType; self. _ suggest = suggest; self. _ concat = concat;} return self;} @ end // WPSuggestResponse. h // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Found Ation/Foundation. h> # pragma mark-h returned result @ interface WPSuggestResponse: NSObject # pragma mark-h attribute @ property (nonatomic, copy) NSString * status; @ property (nonatomic, copy) NSString * errorMessage; @ end // WPSuggestResponse. m // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "WPSuggestResponse. h "@ implementation WPSuggestResponse @ end // // WPUserContentDelegate. h // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Foundation/Foundation. h> @ class WPSuggest; @ class WPSuggestResponse; # pragma mark-h the user sets the protocol the day after tomorrow, which is actually the image extraction method provided by the abstract class in java @ protocol WPUserConsoleDelegate <NSObject> # pragma mark-h submits user feedback, get Feedback result @ required-(WPSuggestResponse *) getWpSuggestResponse :( WPSuggest *) wps Uggest; @ end // WPUserConsoleService. h // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Foundation/Foundation. h> # import "WPUserConsoleDelegate. h "@ interface WPUserConsoleService: NSObject <WPUserConsoleDelegate> @ end // WPUserConsoleService. m // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. All rights reserved. // # import "WPUserConsoleService. h "# import" WPSuggest. h "# import" WPSuggestResponse. h "@ implementation WPUserConsoleService # pragma mark-h create a Request object for feedback-(NSURLRequest *) response :( NSString *) urlStr :( WPSuggest *) wpsuggest {response * requestMM = nil; // URL NSString * str = urlStr; // UTF code str = [str stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncodi Ng]; // create a URL link NSURL * url = [NSURL URLWithString: str];/* Create a variable Request */requestMM = [[NSMutableURLRequest alloc] initWithURL: url cachePolicy :( 0) timeoutInterval: 5.0f]; // set it to Post request [requestMM setHTTPMethod: @ "POST"]; // create the Post parameter NSString * postParamStrs = [NSString stringWithFormat: @ "system =%@ & version =%@ & productType =%@ & suggest =%@ & contact =%@", wpsuggest. _ system, wpsuggest. _ version, wpsuggest. _ productType, wpsugg Est. _ suggest, wpsuggest. _ concat]; // create the parameter object NSData * bodyData = [postParamStrs dataUsingEncoding: NSUTF8StringEncoding]; // set the request parameter [requestMM setHTTPBody: bodyData]; return requestMM ;} # pragma mark-h parse the JSON data returned by the feedback link-(WPSuggestResponse *) getWpSuggestResponse :( WPSuggest *) wpsuggest {WPSuggestResponse * response = nil; // link NSString * str = @ "http: // 192.168.1.126: 18080/wapp // app/addSuggest.htm"; NSMutableU RLRequest * requestMM = (response *) [self getSuggestRequest: str: wpsuggest]; NSData * data = [NSURLConnection sendSynchronousRequest: requestMM failed: nil error: nil]; NSError * error = nil; // serialize the object to the dictionary NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: data options: 0 error: & error]; NSString * status = (NSString *) dic [@ "status"]; NSString * errorMessage = [status isEqualToString: @ "00"]? @ ":( NSString *) dic [@" errorMessage "]; response = [[WPSuggestResponse alloc] init]; response. status = status; response. errorMessage = errorMessage; // convert the object to a string // NSString * aString = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; return response ;}@ end // main. m // OC-API-network access /// Created by wangtouwang on 15/3/24. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Foundation/Foundation. h> # import "WPUserConsoleService. h "# import" WPSuggest. h "int main (int argc, const char * argv []) {@ autoreleasepool {WPUserConsoleService * wpUserConsoleService = [[WPUserConsoleService alloc] init]; WPSuggest * wpsuggest = [[WPSuggest alloc] initWithObject: @ "IOS": @ "8.0": @ "iphone 5 S": @ "test ios Chinese Character transfer 12 ": @ "1312323@qq.com"]; [wpUserConsoleService getWpSuggestResponse: wpsuggest];} return 0 ;}