如何沟通,在Android客户端一个RESTful Web服务 [英] How to communicate to a RESTful Webservice in Android Client

查看:139
本文介绍了如何沟通,在Android客户端一个RESTful Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用(Java的+新泽西州)开发的web服务休息...
该服务需要与即,用户需要填写的Andr​​oid客户端应用程序的格式,并把它发送回休息web服务的地方会被处理,因此用户将得到的输出用户通信...

当我第一次做其余的web服务我已经使用了Web客户端...等能够在表单参数发送和接收请求,作为后和得到...

但如何做同样在Android中,因为没有哪个有这样的属性方法=后

form标签

我已经指示使用XML解析。

更新:我知道如何从REST WEBSERVICE在Android中相互沟通,让DATA但是,​​我们如何将它张贴

其中接受来自Web客户端的响应我的窗体的方法:

  @Path(/制造)
    @POST
    @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XML})
    @Consumes(MediaType.APPLICATION_FORM_URLEN codeD)
    公共无效NEWUSER(@FormParam(的uname)字符串的uname,
            @FormParam(密码)字符串密码,
            @Context HttpServletResponse的ServletResponse的)抛出IOException
        布尔unamec = UserLogin.checkuname(UNAME);
        如果(unamec){
            // 用户已存在
            servletResponse.sendRedirect(HTTP://mysite/Login.html);
        }其他{
            用户登陆U =新用户登陆(UNAME,密码);
            servletResponse.sendRedirec(HTTP:// mysite的/用户/+的uname +/ createnewshop);
        }
    }


解决方案

我看你要求 POST 方法。这里有一个片段,它可以帮助你。

 字符串urlParameters =param_a = A和param_b = B;
        网址URL =新的URL(http://www.yourwebservice.com);
        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();        connection.setDoOutput(真);
        connection.setDoInput(真);
        connection.setInstanceFollowRedirects(假);
        connection.setRequestMethod(POST);
        connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
        connection.setRequestProperty(字符集,UTF-8);
        connection.setRequestProperty(内容长度,+ Integer.toString(urlParameters.getBytes()的长度));
        connection.setUseCaches(假);        //写入的内容到Web服务
        OutputStreamWriter作家=新OutputStreamWriter(connection.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();        //读取从Web服务的答复
        在的BufferedReader =新的BufferedReader(新的InputStreamReader(connection.getInputStream()));
        字符串德codedString;
        而((德codedString = in.readLine())!= NULL){
            returnMsg + =去codedString;
        }        附寄();
        connection.disconnect();

我只会建议你使用异步任务连接到HTTP ,这样你能保证你的Andr​​oid应用将在所有机器人的工作。

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

解决方案

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.

这篇关于如何沟通,在Android客户端一个RESTful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆