簡體   English   中英

如何從android傳遞參數到RESTlet webservice?

[英]how to pass parameters to RESTlet webservice from android?

我一直在網上尋找如何將參數傳遞給RESTlet webservice,但似乎沒有太多關於RESTlet的教程。

我想從我的Android應用程序中的表單發送一些參數(如果我可以使用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 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();
    }

我希望這可能有所幫助

實際上,這取決於你想做什么。 使用REST(請參閱http://en.wikipedia.org/wiki/Representational_state_transfer ),有兩種方法可以傳遞參數或數據。 在您需要了解一些概念之前:

  • 資源:REST實體本身。
  • 表示:對應於其狀態,可以使用不同的HTTP方法獲取或更新。 使用內容類型標頭(Restlet中的媒體類型)標識內容類型。
  • 方法:GET方法用於獲取資源狀態,PUT用於更新它,POST用於創建新資源並同時指定其狀態,DELETE用於刪除資源。

Restlet為REST元素提供Java實體。

因此,在描述之后,您可以看到傳遞數據或參數取決於您的用例:

1°)您想更新資源狀態嗎? 在這種情況下,您將使用POST或PUT等方法使用請求的內容。 數據結構不含text,JSON,XML或二進制文件...... Restlet提供ClientResource類來對RESTful應用程序執行請求。 它還支持構建表示以從接收的數據中發送和提取數據。 在這種情況下,從表單收集的數據將用於構建表示。 以下是一些示例:

//Samples for POST / PUT
ClientResource cr = new ClientResource("http://...");
cr.post(new StringRepresentation("test"));

MyBean bean = new MyBean();
(...)
//Jackson is a tool for JSON format
JacksonRepresentation<MyBean> repr
      = new JacksonRepresentation<MyBean>(bean);
cr.put(repr);

//Samples for GET
Representation repr1 = cr.get();
bean = (new JacksonRepresentation<MyBean>(repr1, MyBean.class)).getObject();

2°)是否要在GET請求中指定參數(例如,將數據配置為retreive等)? 在這種情況下,您只需將其添加到ClientResource上,如下所述:

ClientResource cr = new ClientResource("http://...");
cr.getReference().addQueryParameter("q", "restlet");
Representation repr = cr.get();

在這種情況下,從表單收集的數據將用於構建參數。

希望它能幫到你。 蒂埃里

如果你想要使用json結構的請求和你的響應作為JSONObject,你可以在服務器端這樣做:

public class RequestJSON extends ServerRecource{

  @Post("json")
  public JSONObject testRequest(String entity){
     JSONObject request = new JSONObject(entity);
     String value1 = request.getString("key1");
     int value2    = request.getInt("key2");
     return /* your JSONObject response */;
  }

}

您的要求可以是:

{"key1":"value1", "key2":value2}

我希望這可以幫到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM