我正在尝试将数据发送到服务器,但在服务器连接成功时得到响应为空
它显示以下错误:-
"-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);
}