0

I need to send requests with json data parameters from android app to play framework 1.2.5 web service. I could do it with sending normal paramaters as key value. But I would like to send these parameters as json object. I dont know how to define url in routes and controller static function to handle json request in 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{

    }

}

And here is my routes and controller method

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);
}
demlik
  • 29
  • 3
  • 9

1 Answers1

0

I think you have mistake at declaring Routes and Action in Play apps.

The HTTP response from your android app has one query parameter named jsonRequest. So your action in Play apps should be accepted one query parameter named jsonRequest too. So, in your Play apps, the solution maybe like following :

Routes :

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

Controller :

//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
    ......
}

This post maybe a useful reference for you.

Community
  • 1
  • 1
Wayan Wiprayoga
  • 4,472
  • 4
  • 20
  • 30
  • thanks it worked. I had also a mistake at my httppost header. I should had been like this httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); – demlik Mar 19 '13 at 11:25