尝试将 JSON 数据发送到服务器,但得到“意外的字符串结尾”
Posted
技术标签:
【中文标题】尝试将 JSON 数据发送到服务器,但得到“意外的字符串结尾”【英文标题】:Trying to send JSON data to a server, but getting an "unexpected end of string" 【发布时间】:2013-05-17 04:58:07 【问题描述】:我正在尝试将数据发送到服务器,但在服务器连接成功时得到响应为空
显示以下错误:-
"-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=11
\"Unexpected end of string\" UserInfo=0x4b156c0
NSLocalizedDescription=Unexpected end of string"
)"
我的代码是:--
NSString *post = [NSString stringWithFormat:@"
\"userDetails\":[
\"username\":\"srj.s%2540xxxxxx.com\" ,
\"password\":\"admin123\",
\"apply_class_id\":\"\"],
\"wsfunction\":\"user_authentication\"@"];
//Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//send actual length of your data
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
//Create URLRequest object and initialize it.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
//Set the Url for which your going to send the data to that request.
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://xxxxxxxx.xxxxxxxx.com:8080/xxxxxxxx.php"]]];
//set HTTP method (POST or GET).
[request setHTTPMethod:@"POST"];
//Set HTTP header field with length of the post data.
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//Also set the Encoded value for HTTP header Field.
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
//Set the HTTPBody of the urlrequest with postData.
[request setHTTPBody:postData];
//4. Now, create URLConnection object. Initialize it with the URLRequest.
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
//check that whether you URL connection is done properly or not using just if/else statement as below
if(conn)
NSLog(@"Connection Successful");
else
NSLog(@"Connection could not be made");
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//While receiving the response data
responseData = [responseData appendData:data];
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
//When failed just log
//[delegate hideIndicator];
NSLog(@"Connection failed!");
NSLog(@"Error %@", error);
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//When the response data is downloaded
// NSLog(@" Data obtained %@", responseData);
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(@" Response String %@", responseString);
//converted response json string to a simple NSdictionary
NSMutableArray *results = [responseString JSONValue];
NSLog(@"Response: %@", results);
【问题讨论】:
请发布您从服务器收到的回复。NSString *post = [NSString stringWithFormat:@"
-- 为什么? 如果你只有一个常量字符串,为什么要stringWithFormat:
?
【参考方案1】:
在你的 *post 变量中,
符号末尾有一个不必要的@符号,请尝试删除它并执行您的代码。
另外对于用户名你有 % 符号,希望不会搞砸...
NSString *post = [NSString stringWithFormat:@"\"userDetails\":[\"username\":\"srj.s%2540xxxxxx.com\" , \"password\":\"admin123\",\"apply_class_id\":\"\"],\"wsfunction\":\"user_authentication\""];
【讨论】:
以上是关于尝试将 JSON 数据发送到服务器,但得到“意外的字符串结尾”的主要内容,如果未能解决你的问题,请参考以下文章
将 Graphql 查询作为 JSON 字符串传递时收到意外的令牌错误