23 - 서버 에 JSON, url 코드, 다 중 매개 변 수 를 보 냅 니 다.
- (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);
}
}];
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콘텐츠 SaaS | JSON 스키마 양식 빌더Bloomreach Content를 위한 JSON Form Builder 맞춤형 통합을 개발합니다. 최근 Bloomreach Content SaaS는 내장 앱 프레임워크를 사용하여 혁신적인 콘텐츠 유형 필드를 구축할...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.