android–Java jersey RESTful webservice请求

android–Java jersey RESTful webservice请求,第1张

概述我一直在关注一个关于宁静服务的教程,它运行正常.然而,有一些我还不太了解的东西.这是它的样子:@Path("/hello")publicclassHello{//ThismethodiscalledifTEXT_PLAINisrequest@GET@Produces(MediaType.TEXT_PLAIN)publicStringsayPlainTextH

我一直在关注一个关于宁静服务的教程,它运行正常.然而,有一些我还不太了解的东西.这是它的样子:

@Path("/hello")public class Hello {    // This method is called if TEXT_PLAIN is request    @GET    @Produces( MediaType.TEXT_PLAIN )    public String sayPlainTextHello()     {        return "Plain hello!";    }    @GET    @Produces( MediaType.APPliCATION_JsON )    public String sayJsonTextHello()     {        return "Json hello!";    }    // This method is called if XML is request    @GET    @Produces(MediaType.TEXT_XML)    public String sayXMLHello() {        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";    }    // This method is called if HTML is request    @GET    @Produces(MediaType.TEXT_HTML)    public String sayHTMLHello()     {        return "<HTML> " + "<Title>" + "Hello fittemil" + "</Title>"                + "<body><h1>" + "Hello!" + "</body></h1>" + "</HTML> ";    }} 

困扰我的是我无法利用正确的 *** 作.当我从浏览器请求服务时,会调用相应的sayHTMLHello()方法.但是现在我正在开发一个AndroID应用程序,我希望在Json中得到结果.但是当我从应用程序调用该服务时,将调用MediaType.TEXT_PLAIN方法.我的androID代码看起来与此类似:

Make an HTTP request with android

如何从我的AndroID应用程序中调用使用MediaType.APPliCATION_JsON的方法?
此外,我想使该特定方法返回一个对象,如果我在那里得到一些指导,那将是很好的.

解决方法:

我个人有使用Jersey在Java(JAX-RS)中实现REST的经验.然后我通过AndroID应用程序连接到这个RESTful Web服务.

在AndroID应用程序中,您可以使用http ClIEnt库.它支持http命令,如POST,PUT,DELETE,GET.例如,使用GET命令并以JsON格式或TextPlain传输数据:

public class ClIEnt {    private String server;    public ClIEnt(String server) {        this.server = server;    }    private String getBase() {        return server;    }    public String getBaseURI(String str) {        String result = "";        try {            httpParams httpParameters = new BasichttpParams();            int timeoutConnection = 3000;            httpconnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);            int timeoutSocket = 5000;            httpconnectionParams.setSoTimeout(httpParameters, timeoutSocket);            DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(httpParameters);            httpGet getRequest = new httpGet(getBase() + str);            getRequest.addheader("accept", "application/Json");            httpResponse response = httpClIEnt.execute(getRequest);            result = getResult(response).toString();            httpClIEnt.getConnectionManager().shutdown();        } catch (Exception e) {            System.out.println(e.getMessage());        }         return result;    }    public String getBaseURIText(String str) {        String result = "";        try {            httpParams httpParameters = new BasichttpParams();            int timeoutConnection = 3000;            httpconnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);            int timeoutSocket = 5000;            httpconnectionParams.setSoTimeout(httpParameters, timeoutSocket);            DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(httpParameters);            httpGet getRequest = new httpGet(getBase() + str);            getRequest.addheader("accept", "text/plain");            httpResponse response = httpClIEnt.execute(getRequest);            result = getResult(response).toString();            httpClIEnt.getConnectionManager().shutdown();        } catch (Exception e) {            System.out.println(e.getMessage());        }        return result;    } private StringBuilder getResult(httpResponse response) throws IllegalStateException, IOException {            StringBuilder result = new StringBuilder();            BufferedReader br = new BufferedReader(new inputStreamReader((response.getEntity().getContent())), 1024);            String output;            while ((output = br.readline()) != null)                 result.append(output);            return result;            }}

然后在AndroID类中,您可以:

ClIEnt clIEnt = new ClIEnt("http://localhost:6577/Example/rest/");String str = clIEnt.getBaseURI("Example");    // Json format

解析JsON字符串(或者可能是xml)并在ListVIEw,GrIDVIEw和…中使用它

我简要介绍了你提供的链接.那里有一个好点.您需要在API级别11或更高级别的单独线程上实现网络连接.看看这个链接:HTTP Client API level 11 or greater in Android.

这是我在ClIEnt类中使用http发布对象的方式:

public String postBaseURI(String str, String strUrl) {        String result = "";        try {            httpParams httpParameters = new BasichttpParams();            int timeoutConnection = 3000;            httpconnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);            int timeoutSocket = 5000;            httpconnectionParams.setSoTimeout(httpParameters, timeoutSocket);            DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt(httpParameters);            httpPost postRequest = new httpPost(getBase() + strUrl);            StringEntity input = new StringEntity(str);            input.setContentType("application/Json");            postRequest.setEntity(input);            httpResponse response = httpClIEnt.execute(postRequest);            result = getResult(response).toString();            httpClIEnt.getConnectionManager().shutdown();        } catch (Exception e) {            System.out.println(e.getMessage());        }        return result;    }

在REST WS中,我将对象发布到数据库:

    @POST    @Path("/post")    @Consumes(MediaType.APPliCATION_JsON)    @Produces(MediaType.TEXT_PLAIN)    public Response addTask(Task task) {                Session session = HibernateUtil.getSessionFactory().getCurrentSession();        session.beginTransaction();        session.save(task);        session.getTransaction().commit();        return Response.status(Response.Status.CREATED).build();    }
总结

以上是内存溢出为你收集整理的android – Java jersey RESTful webservice请求全部内容,希望文章能够帮你解决android – Java jersey RESTful webservice请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存