23 - 서버 에 JSON, url 코드, 다 중 매개 변 수 를 보 냅 니 다.

6162 단어 json청구 하 다.
1) 서버 에 제 이 슨 보 내기
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.URL
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"];
    // 2.  
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 3.    
    request.HTTPMethod = @"POST";
    // 4.     (    )
    //            JSON  
    NSDictionary *orderInfo = @{
                                @"shop_id" : @"1243324",
                                @"shop_name" : @"    ",
                                @"user_id" : @"899"
                                };
//       NSData
NSData *json = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = json;

    // 5.     :                ,    JSON  
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    // 6.    
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil || connectionError) return;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        if (error) {
            [MBProgressHUD showError:error];
        } else {
            NSString *success = dict[@"success"];
            [MBProgressHUD showSuccess:success];
        }
    }];
}

2) URL 의 디 코딩 문제 url 에 중국어 가 포함 되 어 있 지 않 기 때문에 중국어 로 디 코딩 해 야 합 니 다.
NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login?username=  &pwd=123"];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

3) 서버 에 다 중 매개 변수 보 내기http://localhost:8080/MJServer/weather?place=beijing&place=shenzhen&place=ss 이런 유형 은 바로 다수 치 매개 변수 다.돌아 오 는 내용 은 {"weathers": [{"city": "beijing", "status": "맑 으 면 구름 이 많아 집 니 다"}, {"city": "shenzhen", "status": "맑 으 면 구름 이 많아 집 니 다"}}} 입 니 다.
코드:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/weather"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //     ff
    request.HTTPMethod = @"POST";
    //      (    )
    NSMutableString *param = [NSMutableString string];
    [param appendString:@"palce=beijing"];
    [param appendString:@"&place=tianjin"];
    [param appendString:@"&place=luyi"];
    param = [NSMutableString stringWithString:[param stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; //     http encode  ,    
    request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];

    //     
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil || connectionError) {
            NSLog(@"HAVE A ERROR");
        }
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dict);
        NSString *error = dict[@"error"];
        if (error) {
            [MBProgressHUD showError:error];
        }else{
            NSLog(@"%@",dict);
        }
    }];
}

좋은 웹페이지 즐겨찾기