无法从Iphone制作Json Request正文 [英] Could not make Json Request body from Iphone

查看:162
本文介绍了无法从Iphone制作Json Request正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.Net软件开发人员。我为用户验证创建了一个WCF Rest API以下是它的签名。

I am a .Net Software developer .I have Created a WCF Rest API for User Validation Following is its signature.

[OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/Validate", Method = "POST")]
    UserListResponse ValidateUser(Login obj);

在fiddler中使用以下Request Body

It is working fine in fiddler with following Request Body

{"Username":"admin",Password:"111"}

我的问题是,无法使用http post方法从Iphone访问它,但它在Android Http post方法中运行良好。

My problem is, it is not accessible from Iphone using http post method but it is working fine in Android Http post method.

以下是我的代码:

NSError *theError = nil;

NSArray *keys = [NSArray arrayWithObjects:@"Username", @"Password", nil];

NSArray *objects = [NSArray arrayWithObjects:@"admin", @"111", nil];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSString *myJSONString =[jsonDictionary JSONRepresentation];

NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"myJSONString :%@", myJSONString); 

NSLog(@"myJSONData :%@", myJSONData); 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate"]];

[request setHTTPBody:myJSONData]; 

[request setHTTPMethod:@"POST"];

NSURLResponse *theResponse =[[NSURLResponse alloc]init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   

NSLog(@"response : %@", theResponse);

NSLog(@"error : %@", theError);

NSLog(@"data : %@", data);

NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"string: %@", string);

NSDictionary *jsonDictionaryResponse = [string JSONValue];

NSLog(@"dic: %@", jsonDictionaryResponse);

以下是我的回复错误:


2012-07-06 11:49:04.201 ShopMonkey [524:207] -JSONRepresentation
失败。错误跟踪是:(null)

2012-07-06 11:49:04.201 ShopMonkey[524:207] -JSONRepresentation failed. Error trace is: (null)

2012-07-06 11:49:04.201 ShopMonkey [524:207] myJSONString
:{密码: 111,用户名:admin}

2012-07-06 11:49:04.201 ShopMonkey[524:207] myJSONString :{"Password":"111","Username":"admin"}

2012-07-06 11:49:04.201 ShopMonkey [524:207] myJSONData:< 7b225061
7373776f 7264223a 22313131 222c2255 7365726e 616d6522 3a226164
6d696e22 7d>

2012-07-06 11:49:04.201 ShopMonkey[524:207] myJSONData :<7b225061 7373776f 7264223a 22313131 222c2255 7365726e 616d6522 3a226164 6d696e22 7d>

2012-07-06 11:51:13.312 ShopMonkey [524:207]回复:(null )

2012-07-06 11:51:13.312 ShopMonkey[524:207] response : (null)

2012-07-06 11:51:13.312 ShopMonkey [524:207]错误:错误
Domain = NSURLErrorDomain Code = -1005网络连接丢失了。
UserInfo = 0x6041370
{NSErrorFailingURLStringKey = http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate,
NSErrorFailingURLKey = http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/验证,
NSLocalizedDescription =网络连接丢失。,
NSUnderlyingError = 0x6043a90网络连接丢失。}

2012-07-06 11:51:13.312 ShopMonkey[524:207] error : Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x6041370 {NSErrorFailingURLStringKey=http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate, NSErrorFailingURLKey=http://192.168.1.192/ShopMonkeyService/ShopMonkey.svc/Validate, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x6043a90 "The network connection was lost."}

2012-07- 06 11:51:13.312 ShopMonkey [524:207]数据:( null)

2012-07-06 11:51:13.312 ShopMonkey[524:207] data : (null)

2012-07-06 11:51:13.313 ShopMonkey [524:207]字符串:

2012-07-06 11:51:13.313 ShopMonkey[524:207] string:

2012-07-06 11:51:13.313 ShopMonkey [524:207] -JSONValue失败。错误
跟踪是:(

2012-07-06 11:51:13.313 ShopMonkey[524:207] -JSONValue failed. Error trace is: (

"Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x6041b90 {NSLocalizedDescription=Unexpected

string of end}的意外结束

end of string}"

2012-07-06 11:51:13.313 ShopMonkey [524:207] dic:(null)

2012-07-06 11:51:13.313 ShopMonkey[524:207] dic: (null)

任何人都可以指出我出了什么问题。

Can anyone point me out what is going wrong.

提前致谢
vijay

Thanks in advance vijay

查看我的错误详情vivek

see my error details vivek

错误详情:

2012-07-06 16:54:37.416 ShopMonkey [1895:40b] - [CJSONSerializer serializeDictionary:]:无法识别选择器发送到实例0x4c03740

2012-07-06 16:54:37.416 ShopMonkey[1895:40b] -[CJSONSerializer serializeDictionary:]: unrecognized selector sent to instance 0x4c03740

2012-07-06 16:54:37.417 ShopMonkey [1895:40b] * 由于未捕获的异常而终止应用'NSInvalidArgumentException',原因:' - [CJSONSerializer serializeDictionary:]:无法识别的选择器发送到实例0x4c03740'

2012-07-06 16:54:37.417 ShopMonkey[1895:40b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CJSONSerializer serializeDictionary:]: unrecognized selector sent to instance 0x4c03740'

* 第一次调用时调用堆栈:

* Call stack at first throw:

0 CoreFoundation 0x00ec2be9 __exceptionPreprocess + 185

0 CoreFoundation 0x00ec2be9 __exceptionPreprocess + 185

1 libobjc.A.dylib 0x010175c2 objc_exception_throw + 47

1 libobjc.A.dylib 0x010175c2 objc_exception_throw + 47

2 CoreFoundation 0x00ec46fb - [NSObject(NSObject)doesNotRecognizeSelector:] + 187

2 CoreFoundation 0x00ec46fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187

3 CoreFoundation 0x00e34366 转发 + 966

3 CoreFoundation 0x00e34366 forwarding + 966

4 CoreFoundation 0x00e33f22 _CF_forwarding_prep_0 + 50

4 CoreFoundation 0x00e33f22 _CF_forwarding_prep_0 + 50

5 ShopMonkey 0x000030c5 - [ShopMonkeyViewController signinbtnclick] + 425

5 ShopMonkey 0x000030c5 -[ShopMonkeyViewController signinbtnclick] + 425

6 UIKit 0x0034fa6e - [UIApplication sendAction:to:from:forEvent:] + 119

6 UIKit 0x0034fa6e -[UIApplication sendAction:to:from:forEvent:] + 119

7 UIKit 0x003de1b5 - [UIControl sendAction:to:forEvent:] + 67

7 UIKit 0x003de1b5 -[UIControl sendAction:to:forEvent:] + 67

8 UIKit 0x003e0647 - [UIControl(Internal)_sendActionsForEvents:withEvent :] + 527

8 UIKit 0x003e0647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527

9 UIKit 0x003df1f4 - [UIControl touchesEnded:withEvent:] + 458

9 UIKit 0x003df1f4 -[UIControl touchesEnded:withEvent:] + 458

10 UIKit 0x003740d1 - [UIWindow _sendTouchesForEvent:] + 567

10 UIKit 0x003740d1 -[UIWindow _sendTouchesForEvent:] + 567

11 UIKit 0x0035537a - [UIApplication sendEvent:] + 447

11 UIKit 0x0035537a -[UIApplication sendEvent:] + 447

12 UIKit 0x003 5a732 _UIApplicationHandleEvent + 7576

12 UIKit 0x0035a732 _UIApplicationHandleEvent + 7576

13 GraphicsServices 0x0177ca36 PurpleEventCallback + 1550

13 GraphicsServices 0x0177ca36 PurpleEventCallback + 1550

14 CoreFoundation 0x00ea4064 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52

14 CoreFoundation 0x00ea4064 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52

15 CoreFoundation 0x00e046f7 __CFRunLoopDoSource1 + 215

15 CoreFoundation 0x00e046f7 __CFRunLoopDoSource1 + 215

16 CoreFoundation 0x00e01983 __CFRunLoopRun + 979

16 CoreFoundation 0x00e01983 __CFRunLoopRun + 979

17 CoreFoundation 0x00e01240 CFRunLoopRunSpecific + 208

17 CoreFoundation 0x00e01240 CFRunLoopRunSpecific + 208

18 CoreFoundation 0x00e01161 CFRunLoopRunInMode + 97

18 CoreFoundation 0x00e01161 CFRunLoopRunInMode + 97

19 GraphicsServices 0x0177b268 GSEventRunModal + 217

19 GraphicsServices 0x0177b268 GSEventRunModal + 217

20 GraphicsServices 0x0177b32d GSEventRun + 115

20 GraphicsServices 0x0177b32d GSEventRun + 115

21 UIKit 0x0035e42e UIApplicationMain + 1160

21 UIKit 0x0035e42e UIApplicationMain + 1160

22 ShopMonkey 0x000026d8 main + 102

22 ShopMonkey 0x000026d8 main + 102

23 ShopMonkey 0x00002669 start + 53

23 ShopMonkey 0x00002669 start + 53

在抛出'NSException'的实例后终止调用

terminate called after throwing an instance of 'NSException'

推荐答案

对于按值传递JSON的密钥你应该进去这个方向..

for Key by value passing in JSON your should go in this direction ..

    NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", @"preference", @"uid", nil];
NSArray *objects = [NSArray arrayWithObjects:@"accuser", @"accpass", @"abc_region", @"100", nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *theURL = [NSURL URLWithString:@"http://url.com/request.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
NSLog(@"%@", theBodyString);
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"%@", theBodyData);
[theRequest setHTTPBody:theBodyData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(theResponseString);
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseString];
NSLog(@"%@", theResponseDictionary);
NSString *theGreeting = [theResponseDictionary objectForKey:@"greeting"];
[self setValue:theGreeting forKey:@"greeting"];

只需检查..
还有一件事你会找到这个文件 CJSONSerializer 从这里
CJSONSerializer文件路径

Just and check .. one more thing u will find this file CJSONSerializer from here CJSONSerializer file path

.................

.................

这篇关于无法从Iphone制作Json Request正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆