0
我需要发送请求json
从android app
到play framework 1.2.5
网络服务的数据参数。我可以通过发送正常参数作为关键值来做到这一点。但我想将这些参数作为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);
}
感谢它的工作。我在我的httppost头上也有一个错误。我应该像这样httppost.addHeader(“Content-Type”,“application/x-www-form-urlencoded”); – demlik 2013-03-19 11:25:09