zoukankan      html  css  js  c++  java
  • 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]; 
  • 相关阅读:
    2018百度之星初赛B轮 rect
    八数码问题(九宫格重排) 利用康托展开 判重
    2018百度之星初赛A轮 度度熊拼三角
    2018百度之星初赛A轮 度度熊学队列
    MongoDB(课时22 唯一索引)
    MongoDB(课时21 索引)
    MongoDB(课时20 游标)
    MongoDB(课时19 数据删除)
    MongoDB(课时18 修改器)
    MongoDB(课时17 更新函数)
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3486889.html
Copyright © 2011-2022 走看看