// util.h
#import <Foundation/Foundation.h>
@interface Util : NSObject {
}
//사이트 내용 NSString으로 리턴 TRUE = POST, FALSE = GET
- (NSString *)stringWithUrl:(NSString*)url setBody:(NSString*)strBody setPOST:(BOOL)Type;
//노드의 내용 리턴
- (NSArray*) arrNodeWithString:(NSString*)stringWithURL NodeName:(NSString*)Node;
@end
// util.m
#import "Util.h"
@implementation Util
//사이트 내용 NSString으로 리턴 TRUE = POST, FALSE = GET
- (NSString *)stringWithUrl:(NSString*)url setBody:(NSString*)strBody setPOST:(BOOL)Type
{
NSMutableURLRequest *urlRequest;
NSData *urlData;
NSURLResponse *response;
NSError *error;
if (Type == TRUE) {
NSLog(@"POST");
NSData *dtBody = [strBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *strBodyLen = [NSString stringWithFormat:@"%d", [dtBody length]];
urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:strBodyLen forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:dtBody];
} else {
NSLog(@"GET");
NSString *strURL = [NSString stringWithFormat:@"%@?%@", url, strBody];
urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
[urlRequest setHTTPMethod:@"GET"];
}
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
// Construct a String around the Data from the response
return [[[NSString alloc] initWithData:urlData encoding:0x80000003] autorelease];
}
//노드의 내용 리턴
- (NSArray*) arrNodeWithString:(NSString*)stringWithURL NodeName:(NSString*)Node;
{
//<node>, </node> 대입할 변수
NSMutableString *FirstNode = [[NSMutableString alloc] init];
NSMutableString *LastNode = [[NSMutableString alloc] init];
[FirstNode setString:@"<"];
[FirstNode appendString:Node];
[FirstNode appendString:@">"];
[LastNode setString:@"</"];
[LastNode appendString:Node];
[LastNode appendString:@">"];
int nodeFirstLen = [FirstNode length]; //<node> length
int nodeLastLen = [LastNode length]; //</node> length
// 토큰, 문자열 잘라서 배열에 저장
NSArray *FirstNodeArray = [stringWithURL componentsSeparatedByString:FirstNode];
NSArray *LastNodeArray = [stringWithURL componentsSeparatedByString:LastNode];
//NSLog(@"FirstNodeArray count = %d", [FirstNodeArray count]);
//NSLog(@"LastNodeArray count = %d", [LastNodeArray count]);
//자른 문자열들의 길이 저장
NSMutableArray *FirstNodeNumArray;
FirstNodeNumArray = [NSMutableArray arrayWithCapacity:[FirstNodeArray count]];
NSMutableArray *LastNodeNumArray;
LastNodeNumArray = [NSMutableArray arrayWithCapacity:[LastNodeArray count]];
int sumFirstLen = 0; //node의 위치
int sumLastLen = 0; //node의 위치
NSString *strLen; //잘려진 문자열
for (int i = 0; i < [FirstNodeArray count]; i++)
{
strLen = [FirstNodeArray objectAtIndex:i];
sumFirstLen += [strLen length] + nodeFirstLen;
//NSLog(@"sumFirstLensumFirstLen = %d", sumFirstLen);
[FirstNodeNumArray addObject:[NSString stringWithFormat:@"%d",sumFirstLen]];
}
for (int i = 0; i < [LastNodeArray count]; i++)
{
if (i == 0) {
strLen = [LastNodeArray objectAtIndex:i];
sumLastLen += [strLen length];
//NSLog(@"sumLastLensumLastLensumLastLen = %d", sumLastLen);
[LastNodeNumArray addObject:[NSString stringWithFormat:@"%d",sumLastLen]];
} else {
strLen = [LastNodeArray objectAtIndex:i];
sumLastLen += [strLen length] + nodeLastLen;
//NSLog(@"sumLastLensumLastLensumLastLen = %d", sumLastLen);
[LastNodeNumArray addObject:[NSString stringWithFormat:@"%d",sumLastLen]];
}
}
/*
int x = [[FirstNodeNumArray objectAtIndex:1] intValue];
int y = [[LastNodeNumArray objectAtIndex:1] intValue];
NSLog(@"x = %d, y = %d", x, y);
NSString *str111 = [[stringWithURL substringFromIndex:x] substringToIndex:y-x];
NSLog(@"str111 = %@", str111);
*/
NSMutableArray *ResultArray; //노드값 저장할 배열
ResultArray = [NSMutableArray arrayWithCapacity:[FirstNodeArray count] -1];
NSString *GetNodeStr;
int x, y;
//드디어 노드값 구해서 배열에 저장
for (int i = 0; i < [FirstNodeArray count] - 1; i++)
{
x = [[FirstNodeNumArray objectAtIndex:i] intValue];
y = [[LastNodeNumArray objectAtIndex:i] intValue];
GetNodeStr = [[stringWithURL substringFromIndex:x] substringToIndex:y-x];
//NSLog(@"GetNodeStr = %@", GetNodeStr);
[ResultArray addObject:[NSString stringWithFormat:@"%@",GetNodeStr]];
}
return ResultArray;
}
@end
// 사용예 =============================================
#import "Util.h"
- (Util*)util
{
return [[Util alloc] init];
}
NSString *str1 = @"http://aaa.com";
NSString *str2 = @"option=aaa&nid=mkapps";
NSString *nnn = [[self util] stringWithUrl:str1 setBody:str2 setPOST:TRUE];
NSArray *aaa = [[self util] arrNodeWithString:nnn NodeName:@"packetTime"];
NSLog(@"%@", aaa);