iOS網(wǎng)絡(luò)之09發(fā)送特殊數(shù)據(jù)到服務(wù)器(json,多值參數(shù))

發(fā)送json到服務(wù)器
1,如果發(fā)送JSON數(shù)據(jù),比如商城下單的時候發(fā)送下單信息給服務(wù)器,則必須在請求頭中說明數(shù)據(jù)類型
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/order"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSDictionary *orderInfo = @{
                                @"shop_id":@"12341",
                                @"shop_name":@"zhang",
                                @"user_id":@"danfeng"
                                };
    
    NSData *data01 = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data01;
    
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
        if(data == nil || connectionError) return ;
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        NSString *success = dict[@"success"];
        
        if(error){
            NSLog(@"%@",error);
        }else{
            NSLog(@"%@",success );
        }
    }];
}
發(fā)送多值參數(shù)到服務(wù)器
2,多值參數(shù),也就是一個參數(shù)中有多個值,直接拼接成字符串放進(jìn)去即可:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/weather"];
    NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    NSMutableString *body = [NSMutableString string];
    [body appendString:@"place=beijing"];
    [body appendString:@"&place=shanghai"];
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if(connectionError || data == nil){
            NSLog(@"request failed");
        }
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        NSString *weather = dict[@"weathers"];
        if(error){
            NSLog(@"%@",error);
        }else if(weather){
            NSLog(@"%@",weather);
        }
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,369評論 6 13
  • 點(diǎn)擊查看原文 Web SDK 開發(fā)手冊 SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個完善的 IM 系統(tǒng)...
    layjoy閱讀 14,306評論 0 15
  • JSON JSON和XML都是需要解析的 JSON是一種輕量級的數(shù)據(jù)格式,一般用于數(shù)據(jù)交互服務(wù)器返回給客戶端的數(shù)據(jù)...
    JonesCxy閱讀 2,005評論 2 10
  • 春風(fēng)響,東風(fēng)重沐暖陽曲 桃花開,綠葉雙并粉紅意 暖陽烘烘,春風(fēng)昭昭 與諸君飲桃花酒與東山之上 看春光暖暖替北風(fēng)寒昭...
    梵夜閱讀 537評論 0 0

友情鏈接更多精彩內(nèi)容