我會用的Restlet得到這個官方的OData頁的數據得到的數據: http://services.odata.org/OData/OData.svc/的Restlet不能與Android
我已經開始這個教程: http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx
我的問題是,最後,他無法獲取元數據。但首先:
當我試圖產生的Restleg檔案(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)
似乎有一個問題與元數據,但我想不通出什麼。 有什麼想法?
我不認爲這與請求的時間有關。在我的情況下,系統拋出一個NetworkOnMainThreadException。把它放在AsyncTask上是正確的做法。 http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Diederik