我试图让Pushy从pushy.me到Xamarin工作。我已经为Pushy创建了一个绑定库,它构建得很好。在Pushy.寄存器(上下文)上,我抛出了一个异常。我不知道是什么导致了异常-是否有其他人尝试将此实现到xamarin中并成功或有任何想法可能出错。
这是扔在
JNIEnv. GetString(class_ref,id_register_Landroid_content_Context_,__args);
谢啦
ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException: android.os.NetworkOnMainThreadException
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/3511/ce955cc0/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
at ME.Pushy.Sdk.Pushy.Register (Android.Content.Context p0) [0x00047] in E:\GitHub\ugs-mobile-app\PushyBindingsLibrary\obj\Debug\generated\src\ME.Pushy.Sdk.Pushy.cs:98
--- End of managed ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException stack trace ---
me.pushy.sdk.util.exceptions.PushyNetworkException: android.os.NetworkOnMainThreadException
at me.pushy.sdk.util.PushyHTTP.post(PushyHTTP.java:61)
at me.pushy.sdk.Pushy.register(Pushy.java:126)
at md52b714a6cf9c1cb241b63b6417cd05e8f.SplashActivity.n_onCreate(Native Method)
at md52b714a6cf9c1cb241b63b6417cd05e8f.SplashActivity.onCreate(SplashActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
static IntPtr id_register_Landroid_content_Context_;
// Metadata.xml XPath method reference: path="/api/package[@name='me.pushy.sdk']/class[@name='Pushy']/method[@name='register' and count(parameter)=1 and parameter[1][@type='android.content.Context']]"
[Register ("register", "(Landroid/content/Context;)Ljava/lang/String;", "")]
public static unsafe string Register (global::Android.Content.Context p0)
{
if (id_register_Landroid_content_Context_ == IntPtr.Zero)
id_register_Landroid_content_Context_ = JNIEnv.GetStaticMethodID (class_ref, "register", "(Landroid/content/Context;)Ljava/lang/String;");
try
{
JValue* __args = stackalloc JValue[1];
__args[0] = new JValue(p0);
string __ret =JNIEnv.GetString(JNIEnv.CallStaticObjectMethod(class_ref,id_register_Landroid_content_Context_, __args),JniHandleOwnership.TransferLocalRef);
return __ret;
}
catch (SystemException e)
{
return null;
}
catch (ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException e)
{
return null;
}
finally
{
}
}
Android. os.Network主线程异常
Android上的网络操作需要在UI主线程外执行,最简单的方法是使用Task
将其推送到默认线程池中的线程上:
await Task.Run(() =>
{
Pushy.Register(context);
});
Task.Run
:将指定的工作排队在ThreadPool上运行,并返回该工作的任务句柄。
参考:https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110). aspx