0

I'm developing some applications and I want to connect them to a Restful WebService. To do that I wrote this class:

public class WebServiceClient {
    public void postService() {
        HttpClient client = new DefaultHttpClient();
        String param1 = "ola\n5\nwww.youtube.com\n1000-01-01_00:00:00.0\n1223";
        String url = "http://localhost:8080/pt.Android.Project.WebService/rest/project";
        HttpPost post = new HttpPost(url);
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("param1", param1));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getService() {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(
                    "http://localhost:8080/pt.Android.Project.WebService/rest/project?id=3");

            HttpResponse response = client.execute(request);

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            String line = "";

            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

The getService method works fine but I can't do posts to my webservice. The interface of my service is:

@Path("/project")
public WebService {
    TagAccessController ctrl;

    public ProjectWebService() {
        ctrl = new TagAccessController();
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getTag(@QueryParam("id") String id) {
//      int id = 1;
        String result = ctrl.getTagContent(id);

        if (result != null) {
            String[] info = result.split("\\|");
            // String [] info = { "funciona","hoje"};

            return "<html> " + "<title>" + "Informação" + "</title>"
                    + "<body><h1>" + info[1] + "</h1>" + "<p>" + info[0]
                    + "</p>" + "<p>" + " Site: " + info[2] + "</p>" + "</body>"
                    + "</html> ";
        } else {
            return "<html> " + "<title>" + "Informação" + "</title>"
                    + "<body><h1>" + "Sem dados a apresentar!" + "</body></h1>"
                    + "</html> ";
        }
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTagText(@QueryParam("id") String id) {
        //int id = 1;
        return ctrl.getTagContent(id);
    }

    @POST
    @Path("/create/{param1}")
    @Produces(MediaType.TEXT_PLAIN)
    public void putTag(@PathParam("param1") String param1{

        //System.out.println(param1);
        ctrl.saveTag(param1);
    }
}

The problem is When I try to do a post only with a test class, the system gives me this message: HTTP Status 405 - Method Not Allowed -------------------------------------------------------------------------------- type Status report message Method Not Allowed description The specified HTTP method is not allowed for the requested resource (Method Not Allowed).

Someone can help me with this?

Sincerely, Rita

Rita
  • 1
  • 1
  • 3

1 Answers1

0

If you are running it on emulator, don't use localhost:

String url = "http://localhost:8080/pt.Android.Project.WebService/rest/project";

Instead try:

String url = "http://10.0.2.2/pt.Android.Project.WebService/rest/project";
  • I'm running in my cellphone.. but the problem is When I try to do a post only with a test class not in my cellphone, the system gives me this message: HTTP Status 405 - Method Not Allowed -------------------------------------------------------------------------------- type Status report message Method Not Allowed description The specified HTTP method is not allowed for the requested resource (Method Not Allowed). – Rita Aug 16 '12 at 14:23