我正在使用以下内容将NSDictionaries数组转换为JSON数据...
创建我的数据......
NSMutableArray *arrayOfDicts = [[NSMutableArray alloc] init];
for (int i = 0; i < 2; i++) {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"MySong", @"title",
@"MyArtist", @"artist",
nil];
[arrayOfDicts addObject:dict];
}
NSArray *info = [NSArray arrayWithArray:arrayOfDicts];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info
options:NSJSONWritingPrettyPrinted error:&error];
Run Code Online (Sandbox Code Playgroud)
然后像这样发送......
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/index.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:jsonData forKey:@"songs"];
[request setDelegate:self];
request.tag = TAG_LOAD_SONGS;
[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)
这是将NSData发送到我的服务器,但是我如何在php中使用它...如果我从我的服务器打印它真的只是一堆随机数,我尝试使用json_encode但我不认为这意味着原始数据...
任何帮助都会很棒!
编辑:这是PHP的响应...
echo $_POST['songs'];
<5b0a2020 7b0a2020 20202274 69746c65 22203a20 224d7953 6f6e6722 2c0a2020 20202261 72746973 7422203a …Run Code Online (Sandbox Code Playgroud) 这里我发送带有一些参数的文件路径或文件.但是服务器没有收到视频.那么代码中的任何问题呢?或者你想要添加的东西.那请告诉我.你想告诉我在哪里遇到错误或设置响应的任何委托方法.
NSString *path = [[NSBundle mainBundle] pathForResource:@"hangover" ofType:@"mp4"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:@"name"];
[request setPostValue:@"GUnit" forKey:@"title"];
[request setPostValue:@"133" forKey:@"user_id"];
[request setPostValue:@"8953d0e1c97ef83c9f0aff47" forKey:@"token"];
[request setPostValue:@"video song" forKey:@"desc"];
[request setPostValue:@"34" forKey:@"video_id"];
[request setPostValue:@"0" forKey:@"is_private"];
[request setFile:path forKey:@"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:@"POST"];
[request startSynchronous];
Run Code Online (Sandbox Code Playgroud)