Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
求助~ios真机上通信序列化异常
Cloud发起了问答2017-11-07
5
回复
704
浏览

SIKI老师求助一下:

我的游戏服务端和前端通信的序列化是按照泰斗破坏神里面的ParameterTool方法做的,使用的是LitJson。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LitJson;

namespace Common.Tools
{
    public class ParameterTool
    {
        public static T GetParameter<T>(Dictionary<byte, object> parameters, ParameterCode parameterCode, bool isObject = true)
        {
            object o = null;
            parameters.TryGetValue((byte)parameterCode, out o);
            if (isObject == false)
            {
                return (T)o;
            }
            return JsonMapper.ToObject<T>(o.ToString());
        }

        public static void AddParameter<T>(Dictionary<byte, object> parameters, ParameterCode key, T value,
            bool isObject = true)
        {
            if (isObject)
            {
                string json = JsonMapper.ToJson(value);
                parameters.Add((byte)key, json);
            }
            else
            {
                parameters.Add((byte)key, value);
            }
        }

现在遇到的问题是,在模拟器上一切正常,但是在IOS真机测试的时候,凡是用到这个序列化工具的通信都不正常。

我查了一下百度,发现这个帖子:http://blog.csdn.net/ak4100/article/details/46880209

麻烦问一下siki老师是不是这个原因,或者您有什么解决思路吗?

十分感谢!

所有回复
  • siki 2017-11-07

    可以尝试下这个插件

    https://www.assetstore.unity3d.com/en/#!/content/11347

    • Cloud 2017-11-09

      siki老师好,我买了这个插件,换上以后在模拟器上一切正常。他的插件里自带了一个link.xml文件,文档里说要把这个文件放在asset目录下,我也照做了。但是在真机上还是报错,看报错的信息是在序列化具体类的时候找不到构造器。(报错信息后附)
      您用过这个插件吧?有遇到类似的问题吗?谢谢。
      报错信息:
      JsonSerializationException: Unable to find a constructor to use for type Common.Model.User. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'ID', line 1, position 6.
      at ActiveAnimation.Finish () [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, Boolean& createdFromNonDefaultCreator) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
      at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <filename unknown>:0
      at Common.Tools.ParameterTool.GetParameter[T] (System.Collections.Generic.Dictionary`2 parameters, ParameterCode parameterCode, Boolean isObject) [0x00000] in <filename unknown>:0
      at LoginRequest.OnOperationResponse (ExitGames.Client.Photon.OperationResponse operationResponse) [0x00000] in <filename unknown>:0
      at PhotonEngine.OnStatusChanged (StatusCode statusCode) [0x00000] in <filename unknown>:0
      at PhotonEngine.OnOperationResponse (ExitGames.Client.Photon.OperationResponse operationResponse) [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.TrafficStatsGameLevel.TimeForResponseCallback (Byte code, Int32 time) [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (System.Byte[] inBuff) [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.EnetPeer.SendOutgoingCommands () [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () [0x00000] in <filename unknown>:0
      at PlayerXmlManager.ClearXmlFile (System.String _name) [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () [0x00000] in <filename unknown>:0
      at PlayerXmlManager.ClearXmlFile (System.String _name) [0x00000] in <filename unknown>:0
      at ExitGames.Client.Photon.PhotonPeer.Service () [0x00000] in <filename unknown>:0
      at BagRequest..ctor () [0x00000] in <filename unknown>:0
      at PhotonEngine.Update () [0x00000] in <filename unknown>:0

      (Filename: currently not available on il2cpp Line: -1)

      (0) 回复
    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2017-11-10

    这个插件老师没用过,是在资源商店上看到的

    . A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute

    这里说 类必须有一个构造方法(带有参数)

    或者使用JsonConstructor 特性 表示的构造方法

    加上这个试下 给 

    • Cloud 2017-11-10

      问题已解决,thx。给这个插件的作者写邮件问了一下,说需要空的构造函数。

      (0) 回复
    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2017-11-10

    关于json在ios上的解析可以尝试下下面的两个插件

    http://blog.csdn.net/kakashi8841/article/details/21877131

    http://download.csdn.net/download/xyb_2012/8771285

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!