我的项目希望部分功能使用Http请求。
服务端代码
[HttpHandler(AppType.Gate, "/")]
public class HttpTest : AHttpHandler
{
[Get("GetAnnouncementList")]
public async ETTask<HttpResult> GetAnnouncementList()
{
var dbProxy = Game.Scene.GetComponent<DBProxyComponent>();
//var infos = await dbProxy.Query<AnnouncementInfo>(_announcement => _announcement.CountNum > 0);
return Ok(data: "asdf");
}
}
客户端代码
using (UnityWebRequestAsync webRequestAsync = ETModel.ComponentFactory.Create<UnityWebRequestAsync>())
{
var versionUrl = "http://localhost:8080/GetAnnouncementList";
await webRequestAsync.DownloadAsync(versionUrl);
var result1 = webRequestAsync.Request.downloadHandler.text;
HttpResult httpResult = MongoHelper.FromJson<HttpResult>(result1);//解析报错
}
结果解析会报错:
FormatException: Invalid JSON input ''.
MongoDB.Bson.IO.JsonScanner.GetNextToken (MongoDB.Bson.IO.JsonBuffer buffer) (at Assets/ThirdParty/MongoDB/MongoDB.Bson/IO/JsonScanner.cs:73)
MongoDB.Bson.IO.JsonReader.PopToken () (at Assets/ThirdParty/MongoDB/MongoDB.Bson/IO/JsonReader.cs:1890)
MongoDB.Bson.IO.JsonReader.ReadBsonType () (at Assets/ThirdParty/MongoDB/MongoDB.Bson/IO/JsonReader.cs:231)
MongoDB.Bson.IO.BsonReader.GetCurrentBsonType () (at Assets/ThirdParty/MongoDB/MongoDB.Bson/IO/BsonReader.cs:129)
MongoDB.Bson.Serialization.BsonClassMapSerializer`1[TClass].Deserialize (MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) (at Assets/ThirdParty/MongoDB/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:88)
MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue] (MongoDB.Bson.Serialization.IBsonSerializer`1[TValue] serializer, MongoDB.Bson.Serialization.BsonDeserializationContext context) (at Assets/ThirdParty/MongoDB/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs:49)
MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType] (MongoDB.Bson.IO.IBsonReader bsonReader, System.Action`1[T] configurator) (at Assets/ThirdParty/MongoDB/MongoDB.Bson/Serialization/BsonSerializer.cs:117)
MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType] (System.String json, System.Action`1[T] configurator) (at Assets/ThirdParty/MongoDB/MongoDB.Bson/Serialization/BsonSerializer.cs:162)
ETModel.MongoHelper.FromJson[T] (System.String str) (at Assets/Model/Base/Helper/MongoHelper.cs:39)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <9577ac7a62ef43179789031239ba8798>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <9577ac7a62ef43179789031239ba8798>:0)
ILRuntime.CLR.Method.CLRMethod.Invoke (ILRuntime.Runtime.Intepreter.ILIntepreter intepreter, ILRuntime.Runtime.Stack.StackObject* esp, System.Collections.Generic.IList`1[T] mStack, System.Boolean isNewObj) (at Assets/ThirdParty/ILRuntime/ILRuntime/CLR/Method/CLRMethod.cs:316)
ILRuntime.Runtime.Intepreter.ILIntepreter.Execute (ILRuntime.CLR.Method.ILMethod method, ILRuntime.Runtime.Stack.StackObject* esp, System.Boolean& unhandledException) (at Assets/ThirdParty/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs:1797)
UnityEngine.Debug:LogException(Exception)
ETModel.Log:Error(Exception) (at Assets/Model/Base/Log.cs:34)
ETModel.AsyncETVoidMethodBuilder:SetException(Exception) (at Assets/Model/Base/Async/AsyncETVoidMethodBuilder.cs:27)
ILRuntime.Runtime.Generated.ETModel_AsyncETVoidMethodBuilder_Binding:SetException_5(ILIntepreter, StackObject*, IList`1, CLRMethod, Boolean) (at Assets/Model/ILBinding/ETModel_AsyncETVoidMethodBuilder_Binding.cs:550)
ILRuntime.Runtime.Intepreter.ILIntepreter:Execute(ILMethod, StackObject*, Boolean&) (at Assets/ThirdParty/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs:1781)
ILRuntime.Runtime.Intepreter.ILIntepreter:Run(ILMethod, Object, Object[]) (at Assets/ThirdParty/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs:96)
ILRuntime.Runtime.Enviorment.AppDomain:Invoke(IMethod, Object, Object[]) (at Assets/ThirdParty/ILRuntime/ILRuntime/Runtime/Enviorment/AppDomain.cs:1086)
ETModel.IAsyncStateMachineAdaptor:MoveNext() (at Assets/Model/Base/IL/IAsyncStateMachineAdaptor.cs:69)
ETModel.MoveNextRunner`1:Run() (at Assets/Model/Base/Async/MoveNextRunner.cs:12)
ETModel.ETTaskCompletionSource:TryInvokeContinuation() (at Assets/Model/Base/Async/ETTaskCompletionSource.cs:57)
ETModel.ETTaskCompletionSource:TrySetResult() (at Assets/Model/Base/Async/ETTaskCompletionSource.cs:90)
ETModel.ETTaskCompletionSource:SetResult() (at Assets/Model/Base/Async/ETTaskCompletionSource.cs:63)
ETModel.UnityWebRequestAsync:Update() (at Assets/Model/Entity/UnityWebRequestAsync.cs:89)
ETModel.UnityWebRequestUpdateSystem:Update(UnityWebRequestAsync) (at Assets/Model/Entity/UnityWebRequestAsync.cs:11)
ETModel.UpdateSystem`1:Run(Object) (at Assets/Model/Base/Object/IUpdateSystem.cs:15)
ETModel.EventSystem:Update() (at Assets/Model/Base/Object/EventSystem.cs:509)
ETModel.Init:Update() (at Assets/Model/Init.cs:57)
但是json的格式实在看不出哪里有问题。
我做了以下测试
HttpResult httpResult2 = new HttpResult
{
code = HttpErrorCode.Success,
msg = "",
status = true,
data = "asdf"
};
var result2 = MongoHelper.ToJson(httpResult2);
HttpResult httpResult2 = MongoHelper.FromJson<HttpResult>(result2);
以上代码可以发序列化成功 没有报错。而且result2 请求服务器返回的 result1 打印出来一摸一样!!!
用compare来对比,呈现为红色,果然有不一样!!
但是不知道是啥不一样,也不知道如何处理,麻烦大家了!!!