iOS : Message = "An error has occurred." in JSON POST
<p><p>在java中工作,而不是在 objective-c 中。尽管处于“已发布 Json 数据”状态,但数据并未保存在数据库中。
我应该以如下所示的格式发送数据以获取 json 响应</p>
<pre><code>{
"ProjID": "78",
"Uid": "12",
"EmailID": "[email protected]",
"ProjectInviterFQAnswers": [{
"slno": "1",
"Answer": "a1",
"order": "1",
"flag": "F"
}, {
"slno": "2",
"Answer": "a1",
"order": "2",
"flag": "F"
}, {
"slno": "1",
"Answer": "a1",
"order": "2",
"flag": "Q"
}
]
};
</code></pre>
<p>我正在发送我登录的字典,格式如下所示。</p>
<p> <img src="/image/TKqKG.png" alt="enter image description here"/> </p>
<p>上面的代码和我的日志截图的区别是';'在每个键值对之后,因此我得到了标题中提到的响应。</p>
<p>有什么建议可以纠正代码/逻辑吗?这是我的代码。</p>
<pre><code> NSError *error = Nil;
NSOperationQueue *queue = [ init];
NSDictionary *dictionaryArray1 = ;
NSDictionary *dictionaryArray2 = ;
NSDictionary *dictionaryArray3 = ;
NSArray *arrayAnswer = ;
NSDictionary *dictionary = ;
NSLog(@"Dictionary that is being sent to URL is = %@", dictionary);
NSURL *url = ;
NSMutableURLRequest *request = [ initWithURL:url];
NSData *requestData = ;
;
;
;
] forHTTPHeaderField:@"Content-Length"];
;
NSURLConnection *connection = [ initWithRequest:request delegate:self];
;
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON Data not posted!");
;
UIAlertView *alertMessage = [ initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
;
}
else
{
;
NSLog(@"JSON data posted! :)");
NSError *error = Nil;
NSJSONSerialization *jsonObject = ;
NSLog(@"Response is %@", jsonObject);
;
}
}];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这就是我解决自己问题的方法。</p>
<pre><code> NSString *str = @"{\"ProjID\": \"78\",\"Uid\": \"12\",\"EmailID\": \"[email protected]\",";
str = ;
str = ;
str = ;
str = ;
str = }"];
NSLog(@"String is === %@", str);
NSURL *url = ;
NSMutableURLRequest *request = [ initWithURL:url];
NSData *requestData = ;
;
;
;
] forHTTPHeaderField:@"Content-Length"];
;
NSURLConnection *connection = [ initWithRequest:request delegate:self];
;
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if(error || !data)
{
NSLog(@"JSON Data not posted!");
;
UIAlertView *alertMessage = [ initWithTitle:@"Error" message:@"Data not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
;
}
else
{
;
NSLog(@"JSON data posted! :)");
NSError *error = Nil;
NSJSONSerialization *jsonObject = ;
NSLog(@"Response is %@", jsonObject);
}
}];
</code></pre></p>
<p style="font-size: 20px;">关于iOS : Message ="An error has occurred." in JSON POST,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19804440/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19804440/
</a>
</p>
页:
[1]