0

我开发了 ac#rest web 服务来与我的 iPad 应用程序通信,到目前为止我一直在使用它没有问题。这是一件非常简单的事情,到目前为止,有几个查询和 GET 方法对我来说效果很好。现在的问题是我正在尝试将数据从我的应用程序发送到 Web 服务,以插入到我的数据库中。

NSString *query = [NSString stringWithFormat:@"http:mywebsite.com"];
    _theURL = [[NSURL alloc]initWithString:query];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:_theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSData *requestData = [NSData dataWithBytes:[json UTF8String] length:[json length]];
    [request setHTTPBody:requestData];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];

这基本上是我的 Objective-C 代码,我尝试建立连接,设置 http 方法来发布和编码我想要发送的 NSData。这个 NSData 只是一个已经可以使用的 json 字符串。

现在在我的 Web 服务中,我尝试使用 Byte[] 获取此 NSData 以解码为字符串,获取我的 json 等等。问题是我做不到。我的网络服务方法代码是:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public bool submitResults(Byte[] data) {
String jsonString = System.Text.Encoding.UTF8.GetString(data);
try{
  JavaScriptSerializer json = new JavaScriptSerializer();
var resultado = json.Deserialize<Pergunta[]>(jsonString);

......它继续。恢复所有这些:我无法将数据发送到 Web 服务,因此我可以将其插入到我的数据库中。任何帮助将不胜感激。

顺便说一句,我得到的错误是这可怕的事情:

{“消息”:“类型 \u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0 .0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]]\u0027 不支持数组的反序列化。","StackTrace":" 在 System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList 列表,类型类型,JavaScriptSerializer在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer 序列化程序, Boolean throwOnError, Object&convertedObject)\r\n 在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal)\r\n .Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer 序列化器,Boolean throwOnError, Object& convertObject)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer。反序列化[T](字符串输入)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

所以,试图帮助你帮助我,这是我的 json : =)

[
  {
    "categoriaID" : 1,
    "cursoID" : "601",
    "perguntaID" : "1",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 1,
    "cursoID" : "601",
    "perguntaID" : "2",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 1,
    "cursoID" : "601",
    "perguntaID" : "3",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 1,
    "cursoID" : "601",
    "perguntaID" : "21",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 2,
    "cursoID" : "601",
    "perguntaID" : "4",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 2,
    "cursoID" : "601",
    "perguntaID" : "5",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 3,
    "cursoID" : "601",
    "perguntaID" : "6",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 3,
    "cursoID" : "601",
    "perguntaID" : "7",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 3,
    "cursoID" : "601",
    "perguntaID" : "8",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 4,
    "cursoID" : "601",
    "perguntaID" : "9",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 4,
    "cursoID" : "601",
    "perguntaID" : "10",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 4,
    "cursoID" : "601",
    "perguntaID" : "11",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 4,
    "cursoID" : "601",
    "perguntaID" : "12",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 4,
    "cursoID" : "601",
    "perguntaID" : "13",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 5,
    "cursoID" : "601",
    "perguntaID" : "14",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 5,
    "cursoID" : "601",
    "perguntaID" : "15",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 5,
    "cursoID" : "601",
    "perguntaID" : "16",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 6,
    "cursoID" : "601",
    "perguntaID" : "17",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 6,
    "cursoID" : "601",
    "perguntaID" : "18",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 6,
    "cursoID" : "601",
    "perguntaID" : "19",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 6,
    "cursoID" : "601",
    "perguntaID" : "20",
    "nb" : 19574,
    "respostaTipo" : "Bom"
  },
  {
    "categoriaID" : 7,
    "cursoID" : "601",
    "perguntaID" : 22,
    "respostaTexto" : "Adorei!",
    "nb" : 19574
  }
]

更新:

经过大量研究,我认为这与我在 Xcode 中的序列化有关。它是这样的:

_arrayPerguntas = [[NSMutableArray alloc]init];
    for (int i = 0; i < _conteudoProgramatico.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:1]];
        [resp setPerguntaID:[_idConteudoProgramatico objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasConteudoProgramatico objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _sessoesPraticas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:2]];
        [resp setPerguntaID:[_idSessoesPraticas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasSessoesPraticas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _materiaisSuporte.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:3]];
        [resp setPerguntaID:[_idMateriaisSuporte objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasMateriaisSuporte objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _apresentacao.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:4]];
        [resp setPerguntaID:[_idApresentacao objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasApresentacao objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _expectativas.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:5]];
        [resp setPerguntaID:[_idExpectativas objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasExpectativas objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    for (int i = 0; i < _feedBack.count; i++) {
        InqueritosResposta *resp = [[InqueritosResposta alloc]init];
        [resp setNb:[NSNumber numberWithInt:_nb]];
        [resp setCursoID:[self indiceSessao:_nomeSessao]];
        [resp setCategoriaID:[NSNumber numberWithInt:6]];
        [resp setPerguntaID:[_idFeedback objectAtIndex:i]];
        [resp setRespostaTipo:[self codeToString:[_respostasFeedback objectAtIndex:i]]];
        [_arrayPerguntas addObject:resp];
    }
    InqueritosResposta *resp = [[InqueritosResposta alloc]init];
    [resp setNb:[NSNumber numberWithInt:_nb]];
    [resp setCursoID:[self indiceSessao:_nomeSessao]];
    [resp setCategoriaID:[NSNumber numberWithInt:7]];
    [resp setPerguntaID:[NSNumber numberWithInt:22]];
    [resp setRespostaTexto:_respostaObservacoes];
    [_arrayPerguntas addObject:resp];

    NSError *writeError = nil;
    NSMutableArray *jsonRespostas = [[NSMutableArray alloc]init];
    for(InqueritosResposta *resp in _arrayPerguntas)
    {
        NSMutableDictionary *jAnswer = [[NSMutableDictionary alloc] init];
        [jAnswer setObject:resp.nb forKey:@"nb"];
        [jAnswer setObject:resp.cursoID forKey:@"cursoID"];
        [jAnswer setObject:resp.categoriaID forKey:@"categoriaID"];
        [jAnswer setObject:resp.perguntaID forKey:@"perguntaID"];
        if (resp.respostaTipo != nil) {
            [jAnswer setObject:resp.respostaTipo forKey:@"respostaTipo"];
        }
        if (resp.respostaTexto != nil) {
            [jAnswer setObject:resp.respostaTexto forKey:@"respostaTexto"];
        }
        [jsonRespostas addObject: jAnswer];
    }
    NSArray *final = [NSArray arrayWithArray:jsonRespostas];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:final options:NSJSONWritingPrettyPrinted error:&writeError];

    [self submitData:jsonData];

基本上我把我所有的对象都放在不同来源的 _arrayPerguntas 中。然后我使用字典将它们全部正确地放在另一个数组(最终数组)中。submitData 方法是我在上面输入的连接设置方法。提前致谢,

快乐编码,

ruitex23

4

2 回答 2

0

Don't do this. Don't use Microsoft's convoluted approach to web services that really only offer a benefit if you're using Microsoft technology to connect to them. Even then it's... shaky at best.

Look into JSONResult MVC Controller Methods. These offer you the most platform independent control over your application's behavior and you won't get any obscure errors like the one you're experiencing. It's pure JSON both ways and there's no guesswork.

于 2012-12-17T19:20:52.247 回答
0

我认为它在反序列化方面失败了。请验证您的 json 对象 (jsonString)。一个非常好的在线 json 验证器是 jsonlint.com

于 2012-12-18T17:11:09.520 回答