2012-08-16 126 views
0

我正在开发一些应用程序,我想将它们连接到Restful WebService。要做到这一点,我写了这个类:如何通过Android应用程序使用HttpClient访问webservice?

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(); 
     } 
    } 
} 

getService方法工作正常,但我不能做我的web服务的职位。我的服务的接口是:

@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); 
    } 
} 

问题是,当我尝试做一个职位只有一个测试类,系统给了我这个消息:HTTP状态405 - 不允许的方法------ -------------------------------------------------- ------------------------ type状态报告消息方法不允许描述指定的HTTP方法不允许用于请求的资源(方法不允许)。

有人可以帮助我吗?

真诚, 丽塔

回答

0

如果是在模拟器上运行它,不要使用localhost:

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

而是尝试:

String url = "http://10.0.2.2/pt.Android.Project.WebService/rest/project"; 
+0

我在我的手机上运行。 。但是问题是当我试图仅仅使用不在我的手机中的测试类来发布帖子时,系统给我这个消息: HTTP状态405 - 方法不是A llowed ----------------------------------------------- --------------------------------- 类型状态报告 消息方法不允许 说明指定的HTTP方法不允许用于请求的资源(方法不允许)。 – Rita 2012-08-16 14:23:51

相关问题