IOS --网络请求

        NSString *reqData = @"";
        NSData *postDatas = nil;
        NSString *urlPath = WEB_SERVICE_LOGIN;
    
        //组json字符串数据
        NSMutableDictionary *mulDic = [[NSMutableDictionary alloc] init];
        [mulDic setValue:@"13551063785" forKey:@"username"];
        [mulDic setValue:@"123" forKey:@"password"];
    
        //NSJSONSerialization 组json字符串
        if ([NSJSONSerialization isValidJSONObject:mulDic]) {
    
            postDatas = [NSJSONSerialization dataWithJSONObject:mulDic options:NSJSONWritingPrettyPrinted error:nil];
            NSString *str = [[NSString alloc] initWithData:postDatas encoding:NSUTF8StringEncoding];
            reqData = [reqData stringByAppendingString:str];
            NSLog(@"reqData:%@",reqData);
    
            postDatas = [NSData dataWithBytes:[reqData UTF8String] length:[reqData length]];
    
        }
    
        NSURL *url = [NSURL URLWithString:urlPath];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
    
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setHTTPBody:postDatas];
    
        NSURLResponse *response;
    
    //    NSData *retData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    //    NSLog(@"vvv:%@",response);
    //    NSString *ret = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
    //    NSLog(@"bbb:%@",ret);
    
        //圈圈单车解析方法
        NSData *retData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *cc = [NSJSONSerialization JSONObjectWithData:retData options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"ccc:%@",cc);


你可能感兴趣的:(IOS --网络请求)