Restlet无法通过Android获取数据

时间:2011-11-29 09:12:29

标签: android metadata odata restlet

我将使用此官方OData 页面的Restlet获取数据: http://services.odata.org/OData/OData.svc/

我已经开始了教程http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx

我的问题是,最后,他无法获取元数据。但首先:

当我尝试生成 Restleg-Files(第5点)时,控制台显示此信息,其中日期的行为红色

---------------------------
OData client code generator
---------------------------
step 1 - check parameters
step 2 - check the ouput directory
step 3 - get the metadata descriptor
Nov 28, 2011 3:12:46 PM org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Nov 28, 2011 3:12:48 PM org.restlet.ext.odata.Service getMetadata
Information: Get the metadata for http://services.odata.org/OData/OData.svc/ at http://services.odata.org/OData/OData.svc/$metadata
Nov 28, 2011 3:12:48 PM org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
step 4 - generate source code
Nov 28, 2011 3:12:48 PM org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Nov 28, 2011 3:12:49 PM org.restlet.ext.odata.Service getMetadata
Information: Get the metadata for http://services.odata.org/OData/OData.svc/ at http://services.odata.org/OData/OData.svc/$metadata
Nov 28, 2011 3:12:49 PM org.restlet.engine.http.connector.HttpClientHelper start
Information: Starting the default HTTP client
Nov 28, 2011 3:12:49 PM org.restlet.engine.http.connector.HttpClientHelper stop
Information: Stopping the default HTTP client
Nov 28, 2011 3:12:50 PM org.restlet.engine.http.connector.HttpClientHelper stop
Information: Stopping the default HTTP client
The source code has been generated in directory: C:\Projects\MyProjects\Android App\main\app\Java\Workspace\TestOData2\src

所以文件是生成的,但是当我在教程中继续,将类复制到我的Android项目中,导入库并编写以下代码,然后我得到错误以下。

public ArrayList<Product> getOdataSampleProduct(String zip) {           

    ODataDemoService proxy = new OdataDemoService();  

    try {       
        Query<Product> query = proxy.createProductQuery("Products(1)");
        Product product = query.iterator().next(); //breaks here
        Log.d("Test","id: "+product.getId()); //isn't logged

    }
    catch(Exception e) {
            //is catched before query.iterator().next()
    };

因此, LogCat 会显示此错误(第3行红色):

...
11-29 09:01:01.039: INFO/org.restlet(700): Starting the default HTTP client
11-29 09:01:01.329: INFO/org.restlet(700): A recoverable error was detected (1001), attempting again in 2000 ms.
11-29 09:01:05.589: ERROR/org.restlet(700): Can't get the metadata for http://services.odata.org/OData/OData.svc/ (response's status: Communication Error (1001) - services.odata.org)
11-29 09:01:05.629: INFO/org.restlet(700): Get the metadata for http://services.odata.org/OData/OData.svc/ at http://services.odata.org/OData/OData.svc/$metadata
11-29 09:01:06.200: INFO/org.restlet(700): Starting the default HTTP client
11-29 09:01:06.329: INFO/org.restlet(700): A recoverable error was detected (1001), attempting again in 2000 ms.
11-29 09:01:06.359: DEBUG/dalvikvm(408): GC_EXPLICIT freed 3K, 5% free 6308K/6595K, paused 3ms+2ms
11-29 09:01:08.409: INFO/org.restlet(700): A recoverable error was detected (1001), attempting again in 2000 ms.
11-29 09:01:10.499: ERROR/org.restlet(700): Can't get the metadata for http://services.odata.org/OData/OData.svc/ (response's status: Communication Error (1001) - services.odata.org)
11-29 09:01:10.579: WARN/org.restlet(700): Can't parse the content of http://services.odata.org/OData/OData.svc/Products(1)
11-29 09:01:10.579: WARN/org.restlet(700): java.lang.Exception: Can't execute the query without the service's metadata.
11-29 09:01:10.579: WARN/org.restlet(700):     at org.restlet.ext.odata.Query.execute(Query.java:347)
11-29 09:01:10.579: WARN/org.restlet(700):     at org.restlet.ext.odata.Query.iterator(Query.java:642)
11-29 09:01:10.579: WARN/org.restlet(700):     at my.folder.testApp.MapView.getData(MapView.java:77)
11-29 09:01:10.579: WARN/org.restlet(700):     at my.folder.testApp.MapView.onCreate(MapView.java:141)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.Activity.performCreate(Activity.java:4397)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.os.Looper.loop(Looper.java:132)
11-29 09:01:10.579: WARN/org.restlet(700):     at android.app.ActivityThread.main(ActivityThread.java:4123)
11-29 09:01:10.579: WARN/org.restlet(700):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 09:01:10.579: WARN/org.restlet(700):     at java.lang.reflect.Method.invoke(Method.java:491)
11-29 09:01:10.579: WARN/org.restlet(700):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-29 09:01:10.579: WARN/org.restlet(700):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-29 09:01:10.579: WARN/org.restlet(700):     at dalvik.system.NativeStart.main(Native Method)

似乎元数据存在问题,但我无法弄清楚是什么。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您是否可以尝试为Apache HTTP Client注册Restlet扩展而不是默认的内部连接器?

有关详细信息,请参阅此页面: http://wiki.restlet.org/docs_2.1/13-restlet/275-restlet/266-restlet.html

答案 1 :(得分:1)

至少,我找到了解决方案: 我不得不将查询放入AsyncTask。 之前,在UI线程中,查询没有时间得到答案。

所以我将它放入doInBackground