0

I have developed a rest webservice using ( Java + Jersey )... the service needs to communicate with the user i.e. the user needs to fill a form in android client app and send it back to rest webservice where it will be processed and accordingly the user will get the output...

When i first made the rest webservice i had used a webclient...and so was able to send and recieve request in form parameters as "post" and "get"...

But how to do the same in android as there is no form tags which have attributes like method="post"

i have been instructed to use XML parsing.

UPDATE: I KNOW HOW TO CONNECT AND GET DATA IN ANDROID FROM REST WEBSERVICE BUT HOW DO WE POST IT ?

My Form method which accepted response from a webclient :

@Path("/create")
    @POST
    @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XML})
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newUser(@FormParam("uname") String uname,
            @FormParam("password") String password,
            @Context HttpServletResponse servletResponse) throws IOException {
        boolean unamec= UserLogin.checkuname(uname);
        if (unamec) {
            // User already exists
            servletResponse.sendRedirect("http://mysite/Login.html");
        } else {
            UserLogin u = new UserLogin(uname, password);
            servletResponse.sendRedirec("http://mysite/users/"+uname+"/createnewshop");
        }
    }
user2416728
  • 400
  • 4
  • 8
  • 25

4 Answers4

1

I see you requested POST methods. Here's a snippet that can help you.

        String urlParameters = "param_a=a&param_b=b";
        URL url = new URL("http://www.yourwebservice.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true); 
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setUseCaches(false);

        // Writes the content to the web service
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();

        // Reads the reply from the web service
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String decodedString;
        while ((decodedString = in.readLine()) != null) {
            returnMsg += decodedString;
        }

        in.close();   
        connection.disconnect();

I would only advise you to connect to HTTP using Async Tasks, this way you guarantee your android app will work in all androids.

Akatosh
  • 448
  • 9
  • 17
0

You need your android client to send HTTP request to your webservice. Take a look at HttpURLConnection class. And here you can find a quick introduction to HTTP client in android.

JEY
  • 6,973
  • 1
  • 36
  • 51
0
Unmarshaller um;
JAXBContext jc = JAXBContext.newInstance(YourClass.class);
um = jc.createUnmarshaller();

URL url = new URL("http://localhost:8080/NutzungscodesMekacodesRESTful/rest/nc/");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
YourClass obj = (YourClass) um.unmarshal(con.getInputStream());
con.disconnect();
model.setXxx(obj.getData());

In Addition you have to annotate your Classes (YourClass.java) in this case. Like that:

@XmlRootElement(name="TagName")
@XmlAccessorType(XmlAccessType.FIELD)
public class YourClassimplements Serializable
{
private static final long serialVersionUID = 1L;
@XmlAttribute
private int ID;
@XmlElement
private String name;
Matthias H
  • 1,300
  • 13
  • 19
0

Have a look at loopj , its built on top of Apaches httpclient and makes network operations a breeze. It lets you work with json and xml, calls are async and it has a get/post params builder so you can easily set up your params. See http://loopj.com/android-async-http/

A quick example as per the doc:

private static final String BASE_URL = "http://api.twitter.com/1/";
private static AsyncHttpClient client = new AsyncHttpClient();

// i've skipped some of the implementation details, but in essence it boils down to this: 
client.post(url, params, responseHandler);
Chris
  • 4,593
  • 1
  • 33
  • 37