IOS 개발 포스트 방식으로 서버 데이터 가져오기

4826 단어 ios 개발
//1. post   url

+(NSString *)createPostURL:(NSMutableDictionary *)params

{

    NSString *postString=@"";

    for(NSString *key in [params allKeys])

    {

        NSString *value=[params objectForKey:key];

        postString=[postString stringByAppendingFormat:@"%@=%@&",key,value];

    }

    if([postString length]>1)

    {

        postString=[postString substringToIndex:[postString length]-1];

    }

    return postString;

}



 

 

//2.zwh - ------post , 

+(NSData *)getResultDataByPost:(NSMutableDictionary *)params

{

 

    NSString *postURL=[Utility createPostURL:params];

    NSError *error;

    NSURLResponse *theResponse;

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:BASEURL]];

    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody:[postURL dataUsingEncoding:NSUTF8StringEncoding]];

    [theRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    return [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&error];

}

 // 

NSMutableDictionary *params=[[NSMutableDictionary alloc] init];

    [params setObject:@"taobao.taobaoke.items.get" forKey:@"method"];

    [params setObject:@"num_iid,title,pic_url,price,score" forKey:@"fields"];

    [params setObject:@" " forKey:@"nick"];

    [params setObject:selectedItemCat.cid forKey:@"cid"];

    [params setObject:@"true" forKey:@"is_mobile"];

    NSData *resultData=[Utility getResultData:params]; 

좋은 웹페이지 즐겨찾기