如何从Android的参数传递给Web服务的Restlet?参数、Android、Restlet、Web

2023-09-08 09:17:23 作者:槍口走火

我一直在寻找网上如何传递参数的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