0

我正在开发一个应用程序,我在其中进行 Json 解析。代码如下:

NSString *urlstring=@"http:www.djksfd.com";
NSURL *url=[NSURL URLWithString:urlstring];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];



NSString *str=[NSString stringWithFormat:@"'{\"keywords\":{\"appList\":[\"Real Drum\",\"Tabla\",\"Adobe Flash Player 11.1\",\"Adobe Reader\",\"Aldiko\"],\"genreMap\":{\"Soundtrack\":\"38\",\"Hindi\":\"182\",\"0\":\"10\",\"Pop\":\"2\",\"Other\":\"4\"}}}'"];

 NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:str forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];


NSHTTPURLResponse* urlResponse = nil; 
NSError *error = [[NSError alloc] init];  
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *result1 = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
// if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) 
//{
NSLog(@"Response: %@", result1); 

响应如下:

 <html><head><title>Apache Tomcat/7.0.27 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>java.lang.NullPointerException
com.mthsense.web.rest.DefaultExceptionMapper.toResponse(DefaultExceptionMapper.java:29)
com.mthsense.web.rest.DefaultExceptionMapper.toResponse(DefaultExceptionMapper.java:1)
org.jboss.resteasy.core.SynchronousDispatcher.executeExceptionMapper(SynchronousDispatcher.java:330)
org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:359)
org.jboss.resteasy.core.SynchronousDispatcher.handleReaderException(SynchronousDispatcher.java:422)
org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:230)
org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:196)
org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:551)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:513)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:125)
org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

note Apache Tomcat/7.0.27 日志中提供了根本原因的完整堆栈跟踪。

Apache Tomcat/7.0.27

.

所以请告诉我如何得到正确的响应,以及如何解决这个错误。

4

2 回答 2

0

好吧,我要做的第一件事是让系统为我完成,而不是手动构建 JSON。如果您正在为 iOS 5+ 进行开发,请查看:

+[NSJSONSerialization dataWithJSONObject:options:error:]

创建NSArrays/NSDictionaries以包含您的对象,然后使用上述方法进行序列化。这将从字典/数组创建您的 JSON 数据。这样您就可以消除 JSON 本身的任何问题。

即使你的最终目标操作系统是 <5.0,这至少会帮助你的调试过程。

哦,你为什么要这样做?

[request setValue:str forHTTPHeaderField:@"Content-Length"];
于 2012-07-03T09:30:12.610 回答
0

我认为你有无效的 json 字符串..请检查一下..在你的 json 字符串中我认为你错过了这个括号“[”..请检查一下......我认为你需要这个 json....

{
"keywords": {
    "appList": [
        "Real Drum",
        "Tabla",
        "Adobe Flash Player 11.1",
        "Adobe Reader",
        "Aldiko"
    ],
    "genreMap": {
        "Soundtrack": "38",
        "Hindi": "182",
        "0": "10",
        "Pop": "2",
        "Other": "4"
       }
    }
}



NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] initWithCapacity:0];

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] initWithCapacity:0];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"Real Drum",@"Tabla",@"Adobe Flash Player 11.1",@"Adobe Reader",@"Aldiko",nill];

[dict2 addObject:arr forKey@"appList"];

NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] initWithCapacity:0];
[dict3 addObject:@"38" forKey:@"Soundtrack"]
[dict3 addObject:@"182" forKey:@"Hindi"]
[dict3 addObject:@"10" forKey:@"0"]
[dict3 addObject:@"4" forKey:@"Other"]
[dict2 addObject:dict3 forKey:@"genreMap"];
[dict2 release];
[dict1 addObject:dict2 forKey:@"keywords"];
[dict2 release];

NSString *jsonString = [jsonWriter stringWithObject:dict1];

NSData *data=[jsonString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:str forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];


NSHTTPURLResponse* urlResponse = nil; 
NSError *error = [[NSError alloc] init];  
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *result1 = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
   // if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) 
   //{
    NSLog(@"Response: %@", result1); 

尝试将其包含在字符串中...

这可能会帮助你..

于 2012-07-03T08:58:18.323 回答