iOS 개발 웹편 - 서버에 json 데이터 및 다중 값 매개 변수 전송
6254 단어 ios 개발
1. 서버에 JSON 데이터 전송
JSON 데이터를 서버에 전송하려면
(1) POST 요청을 사용해야 합니다.
(2) 요청 헤더 설정
(3) JSON 데이터를 요청체로 설정
코드 예:
1 #import "YYViewController.h"
2
3 @interface YYViewController ()
4
5 @end
6
7 @implementation YYViewController
8
9 - (void)viewDidLoad
10 {
11 [super viewDidLoad];
12 }
13
14 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
15 {
16 // 1.
17 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"];
18 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
19 request.HTTPMethod = @"POST";
20
21 // 2.
22 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
23
24 // 3.
25 NSDictionary *json = @{
26 @"order_id" : @"123",
27 @"user_id" : @"789",
28 @"shop" : @"Toll"
29 };
30
31 // NSData --> NSDictionary
32 // NSDictionary --> NSData
33 NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
34 request.HTTPBody = data;
35
36 // 4.
37 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
38 NSLog(@"%d", data.length);
39 }];
40 }
41
42 @end
2. 다수치 매개 변수
다중 값 매개 변수: 하나의 매개 변수는 여러 개의 값에 대응한다.
다음과 같은 요청 매개변수:
http://192.168.1.103:8080/MJServer/weather?place=북경 & place = 하남 & place = 호남
서버의place 속성은 하나의 그룹입니다.따라서 같은 매개 변수로 서버의 값을 덮어쓰지 않습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ios 개발의 다중 루틴 자원 쟁탈전편에서 자주 사용하는 다중 스레드 기술을 소개했는데 현재 개발에서 비교적 자주 사용하는 것은 GCD이고 다른 것은 익숙하면 된다.다중 스레드는 여러 가지 임무를 동시 완성하기 위한 것이지 운행 효율을 높이기 위한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.