如何从Android的参数传递给Web服务的Restlet? [英] how to pass parameters to RESTlet webservice from android?

查看:175
本文介绍了如何从Android的参数传递给Web服务的Restlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找网上如何传递参数的Restlet web服务,但它似乎也没有太大的教程涉及的Restlet。

i've been looking online for how to pass parameters to RESTlet webservice but it seem there are not much tutorial concerning RESTlet.

我想给从形式聚集在我的Andr​​oid应用程序的一些参数(这将是巨大的,如果我能做到这一点使用JSON)。

i would like to send some parameters gathered from a form on my android application (it would be great if i could do this using JSON).

在此先感谢。

推荐答案

以及我解决了这个

作为用于服务器端

@Post
public JSONArray serverSideFunction(Representation entity)
throws JSONException {

    try {
        JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
        System.out.println(req.getString(/* filed name */));
        System.out.println(req.getString(/* filed name */));
        /* 
         * you can retrieve all the fields here 
         * and make all necessary actions
         */
    } catch (IOException e) {
        e.printStackTrace();
    }
}

作为Android的侧

as for the Android Side

 HttpClient client = new DefaultHttpClient();
    String responseBody;
    JSONObject jsonObject = new JSONObject();

    try{
        HttpPost post = new HttpPost(WebService_URL);
        jsonObject.put("field1", ".........");
        jsonObject.put("field2", ".........");

        StringEntity se = new StringEntity(jsonObject.toString());  

        post.setEntity(se);
        post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setHeader("Content-type", "application/json");
        Log.e("webservice request","executing");

        ResponseHandler responseHandler = new BasicResponseHandler();
        responseBody = client.execute(post, responseHandler);
        /* 
         * You can work here on your responseBody
         * if it's a simple String or XML/JSON response
         */
    }
    catch (Exception e) {
        e.printStackTrace();
    }

我希望这可以帮助

I hope this may be of help

这篇关于如何从Android的参数传递给Web服务的Restlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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