首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用post方法发布字典和数组服务器

如何使用post方法发布字典和数组服务器
EN

Stack Overflow用户
提问于 2015-03-02 20:30:26
回答 1查看 581关注 0票数 1

如何发布json服务器我正在尝试添加字典和发布数据,但未获得写入响应

{“type”:“普通”,"noteText":{"lineNo":"1",“lineText”:“患者入院”}}

NSURLSessionConfiguration *configuration = NSURLSessionConfiguration defaultSessionConfiguration;NSURLSession *session = NSURLSession sessionWithConfiguration:配置委派:自委派队列:空;

代码语言:javascript
复制
NSString *Str = @"general";
  NSString *St2r = @"Yogesh";
NSString *Str1 = @"1";

NSMutableArray *临时数组= [NSMutableArray分配数组];

代码语言:javascript
复制
NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                               Str1, @"lineNo",
                             St2r, @"lineText",
                                nil];
[tempArray addObject:tempDictionary];

NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:tempArray,@"noteText", nil];
NSError *error;

NSData *postData = [NSJSONSerialization dataWithJSONObject:tempDict options:0 error:&error];

//请求setHTTPBody:postData;

NSString *jsonRequest = NSString字符串格式:@“{\”类型\“:\”%@\“}”,Str;// NSLog(@“请求:%@",jsonRequest);

代码语言:javascript
复制
NSURL *url7 = [NSURL URLWithString:@"URl"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url7];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPBody:postData];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)

{

    if(error == nil)
    {
        NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
        NSLog(@"Data = %@",text);
    }

}];
EN

回答 1

Stack Overflow用户

发布于 2015-03-02 20:50:06

也许可以考虑使用AFNetworking?

您可以从here下载代码

使用以下代码作为对服务器的post调用的示例。

代码语言:javascript
复制
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28809834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档