开发者

RESTful web service options for Android/Java

开发者 https://www.devze.com 2023-02-07 22:06 出处:网络
First time Java programmer. I\'m wondering what my options are for loading XML on the Android platform (java). There are few docs/demos on connectivity and data loading out there (AFAIK). I need to si

First time Java programmer. I'm wondering what my options are for loading XML on the Android platform (java). There are few docs/demos on connectivity and data loading out there (AFAIK). I need to sign a request with basic authentication and then GET/POST/PUT/DELETE to a URL which will return application/xml content-type. Basically I need to emulate this type of cURL request in Java:

curl  -H 'Content-type: application/xml' -H 'Accept: application/xml' \
      -u开发者_JAVA技巧 '{account}/{username}:{apikey}' https://mywebsite.com

Should I just use the HttpClient in org.apache or is there a better solution? Once I've loaded an XML response from my API, what is the recommeded parser for Android. There seems to be so many parsers for Java, I'm completely overwhelmed.


Certainly use HTTPClient, it should work on Android and it is so much easier to make the calls than writing one yourself... here's an example:-

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }


I prefer the Jersey client myself:

Dependency:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.5</version>
</dependency>

Example:

WebResource resource = Client.create().resource("http://www.site.com/");
ClientResponse response = resource.path("user/list").header("foo", "bar").type(MediaType.TEXT_PLAIN).post("Awesome Sauce!", ClientResponse.class);
String responseBody = response.getEntity(String.class);
System.out.println(responseBody);

But HttpClient would certainly work.

For parsers, I like JAXB, XStream, XPP, JDOM, or there's always the Java DOM API. There are a bunch of XML libraries that you can google. XPP is lightweight and fast, which might work well for Android.


One of the best examples I've seen is the Shelves app from Romain Guy (a Google engineer working on Android):

http://code.google.com/p/shelves/

The app makes HTTP requests to the Google Books service and parses the XML it gets back. There are other examples floating around. But since the Shelves app comes from one of the Google folks I've always had more faith in it displaying best practices than the other code I run across.


The state of the art for invocation of REST services was presented by the author of the official twitter app at Google IO 2010. In terms of parsing XML, I normally use the pull parser built into the SDK.

For a higher-level REST framework, look to Spring Android.

0

精彩评论

暂无评论...
验证码 换一张
取 消