IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

时间:2023-01-31 16:24:56
//
// WPSuggest.h
// OC-API-网络访问
//
// Created by wangtouwang on 15/3/24.
// Copyright (c) 2015年 wangtouwang. All rights reserved.
// #import <Foundation/Foundation.h> #pragma mark -类 反馈信息
@interface WPSuggest : NSObject #pragma mark -属性
@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-网络访问
//
// 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-网络访问
//
// Created by wangtouwang on 15/3/24.
// Copyright (c) 2015年 wangtouwang. All rights reserved.
// #import <Foundation/Foundation.h> #pragma mark -h 反馈信息 返回结果
@interface WPSuggestResponse : NSObject #pragma mark -h 属性
@property(nonatomic,copy) NSString *status;
@property(nonatomic,copy) NSString *errorMessage;
@end //
// WPSuggestResponse.m
// OC-API-网络访问
//
// Created by wangtouwang on 15/3/24.
// Copyright (c) 2015年 wangtouwang. All rights reserved.
// #import "WPSuggestResponse.h" @implementation WPSuggestResponse @end //
// WPUserContentDelegate.h
// OC-API-网络访问
//
// 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 用户后天设置协议 其实就是java中的抽象类 提供的抽像方法
@protocol WPUserConsoleDelegate <NSObject> #pragma mark -h 提交用户反馈信息,获取反馈结果
@required
-(WPSuggestResponse *) getWpSuggestResponse:(WPSuggest *)wpsuggest; @end //
// WPUserConsoleService.h
// OC-API-网络访问
//
// 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-网络访问
//
// 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 创建反馈信息的Request对象
-(NSURLRequest *) getSuggestRequest:(NSString *)urlStr :(WPSuggest *)wpsuggest{
NSMutableURLRequest *requestMM = nil;
// 链接地址
NSString *str =urlStr;
//UTF编码
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//创建URL链接
NSURL *url = [NSURL URLWithString:str]; /*创建可变请求*/
requestMM = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:() timeoutInterval:5.0f];
//设置为Post请求
[requestMM setHTTPMethod:@"POST"];
//创建Post参数
NSString *postParamStrs = [NSString stringWithFormat:@"system=%@&version=%@&productType=%@&suggest=%@&contact=%@",wpsuggest._system,wpsuggest._version,wpsuggest._productType,wpsuggest._suggest,wpsuggest._concat];
//创建参数对象
NSData *bodyData=[postParamStrs dataUsingEncoding:NSUTF8StringEncoding];
//设置请求参数
[requestMM setHTTPBody:bodyData];
return requestMM;
} #pragma mark -h 解析反馈链接返回的JSON数据 -(WPSuggestResponse *) getWpSuggestResponse:(WPSuggest *)wpsuggest{
WPSuggestResponse *response = nil;
// 链接地址
NSString *str = @"http://192.168.1.126:18080/wapp//app/addSuggest.htm";
NSMutableURLRequest *requestMM = (NSMutableURLRequest *)[self getSuggestRequest:str :wpsuggest]; NSData *data =[NSURLConnection sendSynchronousRequest:requestMM returningResponse:nil error:nil];
NSError *error=nil;
//将对象序列化为字典
NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options: error:&error];
NSString *status = (NSString *)dic[@"status"];
NSString *errorMessage =[status isEqualToString:@""]?@"":(NSString *)dic[@"errorMessage"]; response = [[WPSuggestResponse alloc] init];
response.status=status;
response.errorMessage=errorMessage; //将对象转换为字符串
//NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return response;
} @end // main.m
// OC-API-网络访问
//
// 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 5S" :@"TEST IOS 中文传送12" :@"1312323@qq.com"];
[wpUserConsoleService getWpSuggestResponse:wpsuggest];
}
return ;
}

IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一