ios - 如何通过 Post 方法使用 JSON 模型将 JSON 字典作为参数传递?
<p><p>我想使用 <code>uploadUrl</code> 将 JSON 字典作为参数传递,但它给了我一个 <code>Unsupported Url</code> 错误,代码为 1002。</p>
<p>当我在 Postman 上点击此 URL 时,它运行良好。如何使用 JSON 模型实现这一点?</p>
<pre><code>NSString *uploadUrl =@"<Your host URL>";
[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
completion:^(NSDictionary *json, JSONModelError *err)
{
if(err == nil)
{
UIAlertView *alert=[initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
completionHanldler(json);
}
else
{
UIAlertView *alert=[initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
NSMutableDictionary *errorDict=[init];
if(err.code==-1009)
;
else
;
completionHanldler(errorDict);
}
}];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>stringByAddingPercentEscapesUsingEncoding</code></p>
<p>这个方法解决了这个问题。以前我分配了不受支持的 URL。 </p>
<pre><code>NSError * err;
NSData * jsonData = ;
NSString *myString = [ initWithData:jsonData encoding:NSUTF8StringEncoding];
// This line is the answer.
myString = ;
NSString *uploadUrl = ;
[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
completion:^(NSDictionary *json, JSONModelError *err)
{
if(err == nil)
{
UIAlertView *alert=[initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
completionHanldler(json);
}
else
{
UIAlertView *alert=[initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
;
NSMutableDictionary *errorDict=[init];
if(err.code==-1009)
;
else
;
completionHanldler(errorDict);
}
}];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何通过 Post 方法使用 JSON 模型将 JSON 字典作为参数传递?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32367406/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32367406/
</a>
</p>
页:
[1]