0

jsonデータ パラメータを含むリクエストandroid appplay framework 1.2.5Web サービスに送信する必要があります。通常のパラメーターをキー値として送信することでそれを行うことができました。しかし、これらのパラメーターをjsonオブジェクトとして送信したいと思います。で url を定義する方法と、でroutesjson リクエストを処理するコントローラーの静的関数を定義する方法がわかりませんplay framework 1.2.5

public ConnectService(String sngUrl,String searchkey,Double longitude,Double latitude,Double radius){
    try {
        jsonObject.put("searchkey", searchkey);
        jsonObject.put("longitude", longitude); 
        jsonObject.put("latitude", latitude);
        jsonObject.put("radius", radius);
    } catch (JSONException e) {
        System.out.println("HATA 1 : "+e.getMessage());
        e.printStackTrace();
    }

    jArrayParam = new JSONArray();
    jArrayParam.put(jsonObject); 

    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    nameValuePair.add(new BasicNameValuePair("jsonRequest", jsonObject.toString()));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(sngUrl);
    httppost.addHeader("Content-Type", "application/json");         
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8" ));//HTTP.UTF_8   
        System.out.println("URLLLLLLLL : "+httppost.getRequestLine());
        response = httpclient.execute(httppost);                 
        entity = response.getEntity();

    } catch (UnsupportedEncodingException e) {
        System.out.println("HATA 2 : "+e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        System.out.println("HATA 3 : "+e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("HATA 4 : "+e.getMessage());
        e.printStackTrace();
    }
    finally{

    }

}

そして、これが私のルートとコントローラーメソッドです

POST     /search                                       Application.search(jsonRequest)

//not for json request
public static void searchproduct(String searchkey,Double longitude,Double latitude,Double radius){
    String d=searchkey+" "+longitude+" "+latitude+" "+radius ;
    renderJSON(d);
}
4

1 に答える 1

0

Play アプリでの Routes と Action の宣言が間違っていると思います。

Android アプリからの HTTP 応答には、 という名前のクエリ パラメータが 1 つありますjsonRequest。したがって、Play アプリでのアクションは、名前付きの 1 つのクエリ パラメータも受け入れる必要がありますjsonRequest。したがって、Play アプリでは、ソリューションは次のようになります。

ルート:

# Associate to searchproduct action method
POST     /search        Application.searchproduct

コントローラー

//not for json request
public static void searchproduct(String jsonRequest) {
    // convert string to JSON object using org.json.JSONObject
    org.json.JSONObject jsonObject = new org.json.JSONObject(jsonRequest);

    // get all the json element
    String searchkey = jsonObject.getString("searchkey")
    Double radius = jsonObject.getDouble("radius")
    ...... // get the rest element

    // here maybe the rest of logic such as, construct JSON and render
    ......
}

この投稿は、参考になるかもしれません。

于 2013-03-19T00:30:23.917 に答える