问题
I'm going to get data with Restlet of this official OData page: http://services.odata.org/OData/OData.svc/
I've started this tutorial: http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx
My problem is, that at the end, he can't get the metadata. But first:
When I'm trying to generate the Restleg-Files (Point 5), the console is showing this, where the lines with are date are red:
---------------------------
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
So the files are generated, but when I'm going forward in the tutorial, copy the class into my Android project, import libraries and write the following code, then I get the error below.
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()
};
So, LogCat shows this error (line 3 is red):
...
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)
It seems that there is a problem with the metadata, but I can't figure out what. Any idea?
回答1:
Could you try registering the Restlet extension for Apache HTTP Client instead of the default internal connector?
See this page for details: http://wiki.restlet.org/docs_2.1/13-restlet/275-restlet/266-restlet.html
回答2:
At least, I've found a solution:
I had to put the query into an AsyncTask
.
Before, in the UI thread, the query didn't have time to get an answer.
So I put it into the doInBackground
.
来源:https://stackoverflow.com/questions/8308243/restlet-cant-get-the-data-with-android