2013-03-18 6 views
0

android app에서 play framework 1.2.5 웹 서비스로 데이터 매개 변수 json을 요청해야합니다. 나는 평범한 매개 변수를 핵심 가치로 보냄으로써 그것을 할 수 있었다. 하지만 json 개체로 이러한 매개 변수를 보내고 싶습니다. 나는 routes에 URL을 정의하는 방법을 알지 못하며 컨트롤러의 정적 함수는 play framework 1.2.5에서 json 요청을 처리 할 수 ​​없다.재생 프레임 워크에서 json 요청을 처리하는 방법 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); 
} 

답변

0

입니다.

android 앱의 HTTP 응답에 jsonRequest이라는 쿼리 매개 변수가 하나 있습니다. 따라서 Play 앱에서 수행해야 할 작업은 하나의 검색어 매개 변수 jsonRequest에도 허용되어야합니다. 그래서, 당신의 플레이 앱에 솔루션 어쩌면 다음과 같은 :

경로 :

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

This post 어쩌면 유용한 참고.

+0

감사합니다. httppost 헤더에서도 실수를했습니다. 나는이 httppost.addHeader ("Content-Type", "application/x-www-form-urlencoded")와 같아야했다. – demlik

관련 문제