1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

RESTful Web service - Android


Last Updated:

  1. &roid junkie

    &roid junkie New Member This Topic's Starter

    Joined:
    Aug 24, 2010
    Messages:
    2
    Likes Received:
    0
    Hi All,

    I have been following this forum for several weeks now, and finally decided to post my question and see if anybody can help me...

    I want to create an Android App (client-server) that will interact with already running web server.
    (think of something similar like the Facebook App - where the user is able (thru a custom GUI) to view/add/delete photos, edit his blog..etc).
    I want to use the code I already have written for the Website.
    I have decided to use REST as opposed to SOAP, because I am using WCF (which is .svc), and it's more secure for HTTP transfer.

    Here are my 2 questions:


    • What data format is more reliable and simple to use? JSON or Open Data protocol (odata.org) ?
    • How do I make the first communication between the server and the app?
    I need to call the server, and then get a response (just to test the connection).
    Please include some code as an example or a link to where I can lern more!

    Thanks

    &roid junkie
     

    Advertisement
  2. DotNetWebs

    DotNetWebs New Member

    Joined:
    Aug 26, 2010
    Messages:
    1
    Likes Received:
    0
    Hi there.

    First post for me too. I am in similar situation. I have be developing in .net since it's launch but have only just started with Android.

    I have an existing C# application which I have just connected to a WCF web service exposing JSON data to an Android client.

    I have found two sources invaluable in pointing me in the right direction:

    1. The O'Reilly book RESTful .NET by John Flanders.

    and

    2. This blog post:

    Eddie@Blog: Comsuming WCF Services With Android

    I have already had to modify the code above to handle large data streams but it was enough to get me started.

    Regards

    Dotty
     
  3. &roid junkie

    &roid junkie New Member This Topic's Starter

    Joined:
    Aug 24, 2010
    Messages:
    2
    Likes Received:
    0
    Thanks Dotty!
    At the moment I have 4 different Android books, but none of them explain it in detail...
    I will definetly get this book! AND the blog post is very helpful!

    Cheers
     
  4. javarose

    javarose New Member

    Joined:
    Oct 12, 2010
    Messages:
    1
    Likes Received:
    0
    Hi,

    I am having Restful webservice and Android client for Restful Webservice in Java. I sucessfully coded to get the respose using HttpGet() method. but i coudnt how to Post using HttpPost() to Restful Service from Android.

    Here are my Webservice:
    package com.myeclipseide.ws;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.TreeMap;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import com.sun.jersey.spi.resource.Singleton;
    @Produces("application/xml")
    @Path("customers")
    @Singleton
    public class CustomerResource {
    private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();
    public CustomerResource() {
    // hardcode a single customer into the database for demonstration
    // purposes
    Customer customer = new Customer();
    customer.setName("Harold Abernathy");
    customer.setAddress("Sheffield, UK");
    addCustomer(customer);
    }
    @GET
    public List<Customer> getCustomers() {
    List<Customer> customers = new ArrayList<Customer>();
    customers.addAll(customerMap.values());
    return customers;
    }
    @GET
    @Path("{id}")
    public Customer getCustomer(@PathParam("id") int cId) {
    return customerMap.get(cId);
    }
    @POST
    @Path("add")
    @Produces("text/plain")
    @Consumes("application/xml")
    public String addCustomer(Customer customer) {
    int id = customerMap.size();
    customer.setId(id);
    customerMap.put(id, customer);
    return "Customer " + customer.getName() + " added with Id " + id;
    }


    }



    My Android Code

    I dont know how to give the URL for Post Method in HttpPost()

    public​
    void post1()
    {
    // String xml = "<customer><address>1223</address><name>niwin</name></customer>";

    String xml = ​
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

    +​
    "<customers><customer><address>Sheffield, UK</address>"

    +​
    "<id>0</id><name>Harold Abernathy</name></customer></customers>";
    StringEntity se =
    null;
    HttpPost postRequest =
    new HttpPost("http://192.168.2.23:1234/RestfulWS/services/customers/add");
    try {
    se =
    new StringEntity(xml,HTTP.UTF_8);
    se.setContentType(
    "text/xml");
    postRequest.setHeader(
    "Content-Type","application/soap+xml;charset=UTF-8");
    postRequest.setEntity(se);

    HttpClient httpclient =
    new DefaultHttpClient();
    //HttpResponse resp=httpclient.execute(postRequest);


    BasicHttpResponse httpResponse;
    try {
    httpResponse = (BasicHttpResponse) httpclient.execute(postRequest);
    Log.d(
    "HTTPStatus",httpResponse.getStatusLine().toString());
    }
    catch (ClientProtocolException e) {
    // TODO Auto-generated catch block

    e.printStackTrace();
    } ​
    catch (IOException e) {
    // TODO Auto-generated catch block

    e.printStackTrace();
    }



    &#12288;
    } ​
    catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block

    Log.d(​
    "UnsupportedEncodingException ","Resp:\n"+e.getMessage());
    e.printStackTrace();
    }


    The above programs execute with error free but i am not getting the Posted values in Webservice
    Please Help me how to overcome this one?
     
  5. harishandro

    harishandro New Member

    Joined:
    Oct 19, 2011
    Messages:
    1
    Likes Received:
    0
    can anyone tell how to use webservices in android ..suggest any book
     

Share This Page

Loading...