IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一

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

 2015/3/24 21:34:10  ak南昌小黑  程序员俱乐部  我要评论(0)
  • 摘要:////WPSuggest.h//OC-API-网络访问////Createdbywangtouwangon15/3/24.//Copyright(c)2015年wangtouwang.Allrightsreserved.//#import<Foundation/Foundation.h>#pragmamark-类反馈信息@interfaceWPSuggest:NSObject#pragmamark-属性@property(nonatomic,copy
  • 标签:
//
//  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:(0) 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: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;
   
    //将对象转换为字符串
    //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 0;
}

 

  • 相关文章
发表评论
用户名: 匿名