Xamarin - 尝试读取JSon时出错

时间:2015-07-04 09:52:26

标签: c# php android json xamarin

我希望我的Android应用程序能够使用Json从站点获取信息,并且我使用Rest API发送和接收该信息,但是事情不能按预期工作。 它给了我一个java.lang.reflect.InvocationTargetException,调试输出如下:

Forwarding debugger port 8842
Detecting existing process
Loaded assembly: /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll
Loaded assembly: /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll [External]
Loaded assembly: Mono.Android.dll [External]
[art] Late-enabling -Xcheck:jni
[monodroid] Using override path: /data/data/eu.COMPANY_NAME.homecheckapp/files/.__override__
[monodroid] Using override path: /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__
[monodroid] Trying to load sgen from: /data/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/libmonosgen-2.0.so
[monodroid] Trying to load sgen from: /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/libmonosgen-2.0.so
[monodroid] Trying to load sgen from: /data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libmonosgen-2.0.so
[monodroid] Trying to load sgen from: /data/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/links/libmonosgen-2.0.so
[monodroid-debug] Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8842,server=y,embedding=1
[Mono] Image addref mscorlib[0x41dd99b8] -> mscorlib.dll[0x60df5ac8]: 1
[Mono] AOT module 'mscorlib.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-mscorlib.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/mscorlib.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-mscorlib.dll.so" not found
[Mono] Assembly mscorlib[0x41dd99b8] added to domain RootDomain, ref_count=1
[Mono] Assembly Loader probing location: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll'.
[Mono] Image addref HomecheckApp[0x5aa78f38] -> /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll[0x5aa782d8]: 2
[Mono] Assembly HomecheckApp[0x5aa78f38] added to domain RootDomain, ref_count=1
[Mono] AOT module '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-HomecheckApp.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/HomecheckApp.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-HomecheckApp.dll.so" not found
[Mono] Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll'.
[Mono] Config attempting to parse: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/HomecheckApp.dll.config'.
[Mono] Config attempting to parse: '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/HomecheckApp/HomecheckApp.config'.
[Mono] Assembly Loader probing location: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll'.
[Mono] Image addref RestSharp.MonoDroid[0x5aa79e60] -> /storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll[0x5aa79218]: 2
[Mono] Assembly RestSharp.MonoDroid[0x5aa79e60] added to domain RootDomain, ref_count=1
[Mono] AOT module '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-RestSharp.MonoDroid.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/RestSharp.MonoDroid.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-RestSharp.MonoDroid.dll.so" not found
[Mono] Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll'.
[Mono] Config attempting to parse: '/storage/emulated/0/Android/data/eu.COMPANY_NAME.homecheckapp/files/.__override__/RestSharp.MonoDroid.dll.config'.
[Mono] Config attempting to parse: '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/etc/mono/assemblies/RestSharp.MonoDroid/RestSharp.MonoDroid.config'.
[monodroid-gc] GREF GC Threshold: 46080
[Mono] Image addref Mono.Android[0x5aa7aaf8] -> Mono.Android.dll[0x5aa79f80]: 1
[Mono] Assembly Mono.Android[0x5aa7aaf8] added to domain RootDomain, ref_count=1
[Mono] AOT module 'Mono.Android.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-Mono.Android.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/Mono.Android.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-Mono.Android.dll.so" not found
[Mono] Assembly Ref addref Mono.Android[0x5aa7aaf8] -> mscorlib[0x41dd99b8]: 2
[Mono] DllImport attempting to load: '__Internal'.
[Mono] DllImport loaded library '(null)'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_lref_log_new'.
[Mono] Probing '_monodroid_lref_log_new'.
[Mono] Found as '_monodroid_lref_log_new'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_lref_log_delete'.
[Mono] Probing '_monodroid_lref_log_delete'.
[Mono] Found as '_monodroid_lref_log_delete'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gref_log_new'.
[Mono] Probing '_monodroid_gref_log_new'.
[Mono] Found as '_monodroid_gref_log_new'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_get_identity_hash_code'.
[Mono] Probing '_monodroid_get_identity_hash_code'.
[Mono] Found as '_monodroid_get_identity_hash_code'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gref_log'.
[Mono] Probing '_monodroid_gref_log'.
[Mono] Found as '_monodroid_gref_log'.
[Mono] Image addref System.Core[0x5f2e2958] -> System.Core.dll[0x5f2e1f70]: 1
[Mono] Assembly System.Core[0x5f2e2958] added to domain RootDomain, ref_count=1
[Mono] AOT module 'System.Core.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.Core.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Core.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.Core.dll.so" not found
[Mono] Assembly Ref addref Mono.Android[0x5aa7aaf8] -> System.Core[0x5f2e2958]: 2
[Mono] Assembly Ref addref System.Core[0x5f2e2958] -> mscorlib[0x41dd99b8]: 3
Loaded assembly: System.Core.dll [External]
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gref_log_delete'.
[Mono] Probing '_monodroid_gref_log_delete'.
[Mono] Found as '_monodroid_gref_log_delete'.
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for '_monodroid_gc_wait_for_bridge_processing'.
[Mono] Probing '_monodroid_gc_wait_for_bridge_processing'.
[Mono] Found as '_monodroid_gc_wait_for_bridge_processing'.
[monodroid] Xamarin/Android Trial Mode Active
[OpenGLRenderer] Use EGL_SWAP_BEHAVIOR_PRESERVED: false
[Atlas] Validating map...
[libEGL] loaded /system/lib/egl/libEGL_tegra.so
[libEGL] loaded /system/lib/egl/libGLESv1_CM_tegra.so
[libEGL] loaded /system/lib/egl/libGLESv2_tegra.so
[OpenGLRenderer] Initialized EGL, version 1.4
[OpenGLRenderer] Enabling debug mode 0
[Mono] Assembly Ref addref HomecheckApp[0x5aa78f38] -> Mono.Android[0x5aa7aaf8]: 2
Loaded assembly: MonoDroidConstructors [External]
[Mono] Assembly Ref addref HomecheckApp[0x5aa78f38] -> mscorlib[0x41dd99b8]: 4
[Mono] Image addref System.Json[0x61398b60] -> System.Json.dll[0x614b24a8]: 1
[Mono] Assembly System.Json[0x61398b60] added to domain RootDomain, ref_count=1
[Mono] AOT module 'System.Json.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.Json.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.Json.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.Json.dll.so" not found
[Mono] Assembly Ref addref HomecheckApp[0x5aa78f38] -> System.Json[0x61398b60]: 2
[Mono] Assembly Ref addref System.Json[0x61398b60] -> mscorlib[0x41dd99b8]: 5
Loaded assembly: System.Json.dll [External]
[Mono] Image addref System[0x614bf570] -> System.dll[0x614dd030]: 1
[Mono] Assembly System[0x614bf570] added to domain RootDomain, ref_count=1
[Mono] AOT module 'System.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.dll.so" not found
[Mono] AOT module '/Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/builds/install/mono-armv7/lib/mono/aot-cache/arm/System.dll.so' not found: dlopen failed: library "/data/app/eu.COMPANY_NAME.homecheckapp-1/lib/arm/libaot-System.dll.so" not found
[Mono] Assembly Ref addref HomecheckApp[0x5aa78f38] -> System[0x614bf570]: 2
[Mono] Assembly Ref addref System[0x614bf570] -> mscorlib[0x41dd99b8]: 6
Loaded assembly: System.dll [External]
[Mono] Assembly Ref addref Mono.Android[0x5aa7aaf8] -> System[0x614bf570]: 3
Thread started:  #2
Thread started: <Thread Pool> #3
    Thread started: <Thread Pool> #4
        [Mono] DllImport attempting to load: '/system/lib/libc.so'.
        [Mono] DllImport loaded library '/system/lib/libc.so'.
        [Mono] DllImport searching in: '/system/lib/libc.so' ('/system/lib/libc.so').
        [Mono] Searching for 'gettid'.
        [Mono] Probing 'gettid'.
        [Mono] Found as 'gettid'.
        [Mono] DllImport attempting to load: '/system/lib/liblog.so'.
        [Mono] DllImport loaded library '/system/lib/liblog.so'.
        [Mono] DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
        [Mono] Searching for '__android_log_print'.
        [Mono] Probing '__android_log_print'.
        [Mono] Found as '__android_log_print'.
        [MonoDroid] UNHANDLED EXCEPTION:
        [MonoDroid] System.Net.WebException: Error: NameResolutionFailure
        [MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00078>
            [MonoDroid] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0006b>
                    [MonoDroid] at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
                        [MonoDroid] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
                        [MonoDroid] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Java.Lang.IRunnable.cs:71
                        [MonoDroid] at (wrapper dynamic-method) object.64d78430-69a4-4af6-bd73-cd5a21882790 (intptr,intptr) <IL 0x00011, 0x0003b>
                            [art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
                            [AndroidRuntime] Shutting down VM

处理Json共享的活动:

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

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Json;
using System.Net;
using System.IO;
using System.Threading.Tasks;

namespace HomecheckApp {
    [Activity(Label = "LoginActivity", MainLauncher = true)]            
    public class LoginActivity : Activity {
        protected override void OnCreate(Bundle bundle) {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Login);
            EditText email = FindViewById<EditText>(Resource.Id.editEmail);
            EditText password = FindViewById<EditText>(Resource.Id.editPassword);
            Button loginButton = FindViewById<Button>(Resource.Id.loginButton);
            loginButton.Click += async (sender, e) => {
                string url = "http://homecheck/appapi/finduser";
                JsonValue json = await FetchUserAsync(url);
            };
        }

        private async Task<JsonValue> FetchUserAsync(string url) {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            request.ContentType = "application/json";
            request.Method = "GET";
            using(WebResponse response = await request.GetResponseAsync()) {
                using(Stream stream = response.GetResponseStream()) {
                    JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                    Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());
                    return jsonDoc;
                }
            }
        }

    }

}

最后MVC控制器中的动作:

 public function finduserAction() {
    $this->getResponse()
        ->setHeader('Content-Type', 'application/json');
    $response = array('name' => 'test', 'version' => '1.0');
    $this->view->json = json_encode($response);
}

视图中显示的内容:

enter image description here

如果我输入一个返回Json的不同string url(例如http://echo.jsontest.com/key/value/one/two)它可以工作,那么问题可能就在控制器的某个地方了吗?

1 个答案:

答案 0 :(得分:1)

正如其他人已经提到的那样,您正在调试应用的设备无法解析“http://homecheck/appapi/finduser”,因为它不知道主机的主页检查。

Probaby您在本地运行API应用程序。尝试将其部署到公共服务器或设置本地DNS以将homecheck路由到运行API应用程序的计算机。

如果您可以在设置中提供一些信息,例如你使用真正的Android设备,运行API应用程序的Web服务器,我们可以帮助你解决问题。