android post请求时报415错误,post请求传递JSON数据类型(415错误解决)

3995ba473524

post请求传递JSON数据类型

报错:JSON text did not start with array or object and option to allow fragments not set

问题一:如何上传JSON类型的数据

解答:

NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc] init];

[jsonDic setObject:self.model.treeid forKey:@"treeId"];

[jsonDic setObject:self.caseId forKey:@"caseId"];

[jsonDic setObject:self.imageArray forKey:@"pictureMD5s"];

NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:0 error:&error];

NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSString *urlStr = [NSString stringWithFormat:@"%s/case/pat/picture/create",APPURL];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlStr]];

request.HTTPMethod = @"POST";

request.HTTPBody = [json dataUsingEncoding:NSUTF8StringEncoding];

以上是上传部分

问题二:=Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Error 415 Unsupported Media Type

HTTP ERROR 415

解答:

需要设置Content-Type这个字段@"application/json;charset=utf-8"(必须加utf-8)

将请求头的Content-Type  设置为@"application/json;charset=utf-8"类型,满足该接口的请求头规范

[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];

就是因为没加;charset=utf-8,卡住死活解决不了,难受。。。。。。。。。

不过终于解决了。

😁 有帮助到你就点个赞呗!😁

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值