我需要将带有json
数据参数的请求发送android app
到play framework 1.2.5
Web 服务。我可以通过发送普通参数作为键值来做到这一点。但我想将这些参数作为 json 对象发送。我不知道如何定义 url inroutes
和控制器静态函数来处理 json 请求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);
}