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)

根本原因の完全なスタックトレースは、Apache Tomcat/7.0.27ログで入手できます。

Apache Tomcat / 7.0.27

では、正しい応答を得る方法と、このエラーを解決する方法を教えてください。

4

2 に答える 2

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 に答える
0

さて、JSONを手動で作成するのではなく、最初に行うことは、システムにそれを行わせることです。iOS 5以降向けに開発している場合は、以下をご覧ください。

+[NSJSONSerialization dataWithJSONObject:options:error:]

オブジェクトを含むNSArrays/を作成してから、上記の方法でシリアル化します。NSDictionariesこれにより、辞書/配列からJSONデータが作成されます。そうすれば、JSON自体の問題を排除できます。

最終的なターゲットOSが5.0未満の場合でも、これは少なくともデバッグプロセスに役立ちます。

ああ、なぜあなたはこれをしているのですか?

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