如何发布json服务器我正在尝试添加字典和发布数据,但未获得写入响应
{“type”:“普通”,"noteText":{"lineNo":"1",“lineText”:“患者入院”}}
NSURLSessionConfiguration *configuration = NSURLSessionConfiguration defaultSessionConfiguration;NSURLSession *session = NSURLSession sessionWithConfiguration:配置委派:自委派队列:空;
NSString *Str = @"general";
NSString *St2r = @"Yogesh";
NSString *Str1 = @"1";NSMutableArray *临时数组= [NSMutableArray分配数组];
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);
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);
}
}];发布于 2015-03-02 20:50:06
也许可以考虑使用AFNetworking?
您可以从here下载代码
使用以下代码作为对服务器的post调用的示例。
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);
}];https://stackoverflow.com/questions/28809834
复制相似问题