IOS开发之Post 方式获取服务器数据

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

 

你可能感兴趣的:(ios开发)