IOS 學習筆記 2015-03-24 OC-API-網路訪問-案例一,2015-03-24oc-api-

來源:互聯網
上載者:User

IOS 學習筆記 2015-03-24 OC-API-網路訪問-案例一,2015-03-24oc-api-

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.