500内部服务器错误GAE试图JSON字符串发送给客户端Android版? [英] 500 Internal Server Error GAE while trying to send JSON string to client android side?
问题描述
我在谷歌应用程序引擎的工作存储data.My应用程序是一个Android应用程序,我想,当我想收到的ArrayList作为JSON文本接收来自GAE数据存储side.However DATAS,我得到错误:500内部服务器错误的消息,我无法找到有关我把我的客户端和服务器端的codeS这里的任何解决方案;
服务器端
块引用>@燮pressWarnings(串行)
公共类ReceiveLoc延伸的HttpServlet {
公共静态最后弦乐USER_ID =用户id;
公共静态最后弦乐USER_LATITUDE =纬度;
公共静态最后弦乐USER_LONGITUDE =经度;
@覆盖
保护无效的doPost(HttpServletRequest的REQ,HttpServletResponse的RESP)
抛出了ServletException,IOException异常{
// TODO自动生成方法存根
字符串USER_ID = req.getParameter(USER_ID);
resp.setContentType(应用/ JSON);
PrintWriter的OUT = resp.getWriter();
通过out.println(receiveLoc(USER_ID));
了out.flush();
}
私人字符串receiveLoc(字符串USER_ID){
// TODO自动生成方法存根
ArrayList的<地点>位置=新的ArrayList< ReceiveLoc.Location>();
DatastoreService数据存储= DatastoreServiceFactory.getDatastoreService();
过滤filterId =新的过滤器predicate(USER_ID,Query.FilterOperator.EQUAL,USER_ID);
查询Q =新的查询(UserLoc);
q.setFilter(filterId); preparedQuery PQ =数据存储prepare(Q)。 为(实体结果:pq.asIterable()){
双纬度=(双)result.getProperty(USER_LATITUDE);
双经度=(双)result.getProperty(USER_LONGITUDE);
locations.add(Location.newInstance(纬度,经度));
}
类型type =新TypeToken< ArrayList的<地点>>(){} .getType();
GSON GSON =新GSON();
JSON字符串= gson.toJson(位置,类型);
返回JSON;
}静态类位置{
双纬度;
双经度; 公共场所(双纬度,经度双){
this.latitude =纬度;
this.longitude =经度;
}
公共双getLatitude(){
返回纬度;
}
公共双getLongitude(){
返回经度;
}
公共静态的newInstance位置(双纬度,经度双){
返回新的位置(经度,纬度);
}
}
}
客户端
块引用>类ClientAsyncReceiveLoc扩展的AsyncTask<弦乐,太虚,字符串> {
//这个AsyncTask的类,因为该类创建从ClientAsync类不同,
//位置(双)值进行处理。
活动活动;
//构造函数。
公共ClientAsyncReceiveLoc(活动活动){
this.activity =活动;
} @覆盖
保护字符串doInBackground(字符串... user_ID的){
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(https://application-id.appspot.com/+ MainActivity.OPERATION_RECEIVE_LOC);
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<>(1);
nameValuePairs.add(新BasicNameValuePair(User.USER_ID,USER_ID [0])); 尝试{
httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpClient.execute(httpPost);
Log.i(连接响应code,将String.valueOf(response.getStatusLine()的getStatus code()));
如果(response.getStatusLine()的getStatus code()== 200){
HttpEntity实体= response.getEntity();
返回EntityUtils.toString(实体);
}
回归错误:+响应
.getStatusLine()
.getStatus code()++响应
。.getStatusLine()getReasonPhrase();
}赶上(MalformedURLException的E){
返回e.getMessage();
}赶上(UnsupportedEncodingException五){
返回e.getMessage();
}赶上(IOException异常五){
返回e.getMessage();
}
}
@覆盖
保护无效onPostExecute(字符串jsonResponse){
//super.onPostExecute(s);
Log.i(RECEIVED,jsonResponse);
}
}
解决方案虽然我加入到GSON外部库到我的项目,我只是做了一步即 RightClickProject>构建路径>配置构建路径(库选项卡) - >添加外部JAR 的步骤,我忘了这些指令的将其放置在战争/ WEB-INF / lib下的谷歌Web项目的文件夹这就是为什么,当我想收到的感谢DATAS从GSON服务器端项目中,我得到了 500内部服务器错误作为响应消息。根据这一点,我建议大家谁得到这些问题,要小心而增加任何外部库到您的项目。
链接这是帮助我解决问题的链接。
I am working on Google App Engine to store data.My application is a android application and I want to receive datas from GAE datastore side.However when I want to receive ArrayList as a JSON text , I am getting "Error : 500 Internal Server Error" message and I could not find any solution about that I put my client and server side codes here ;
Server Side
@SuppressWarnings("serial") public class ReceiveLoc extends HttpServlet{ public static final String USER_ID = "userId"; public static final String USER_LATITUDE = "latitude"; public static final String USER_LONGITUDE = "longitude"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String user_id = req.getParameter(USER_ID); resp.setContentType("application/json"); PrintWriter out = resp.getWriter(); out.println(receiveLoc(user_id)); out.flush(); } private String receiveLoc(String user_id) { // TODO Auto-generated method stub ArrayList<Location> locations = new ArrayList<ReceiveLoc.Location>(); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Filter filterId = new FilterPredicate(USER_ID, Query.FilterOperator.EQUAL,user_id); Query q = new Query("UserLoc"); q.setFilter(filterId); PreparedQuery pq = datastore.prepare(q); for(Entity result: pq.asIterable()){ double latitude = (double) result.getProperty(USER_LATITUDE); double longitude = (double) result.getProperty(USER_LONGITUDE); locations.add(Location.newInstance(latitude, longitude)); } Type type = new TypeToken<ArrayList<Location>>() {}.getType(); Gson gson = new Gson(); String json = gson.toJson(locations,type); return json; }static class Location { double latitude; double longitude; public Location(double latitude,double longitude){ this.latitude = latitude; this.longitude = longitude; } public double getLatitude() { return latitude; } public double getLongitude() { return longitude; } public static Location newInstance(double latitude,double longitude){ return new Location(latitude, longitude); } } }
Client Side
class ClientAsyncReceiveLoc extends AsyncTask<String,Void,String> { //This ASYNCTASK class created different from ClientAsync class because in that class, //locations(double) values are processed. Activity activity; //constructor. public ClientAsyncReceiveLoc(Activity activity) { this.activity = activity; } @Override protected String doInBackground(String... user_id) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("https://application-id.appspot.com/" + MainActivity.OPERATION_RECEIVE_LOC); List<NameValuePair> nameValuePairs = new ArrayList<>(1); nameValuePairs.add(new BasicNameValuePair(User.USER_ID, user_id[0])); try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpPost); Log.i("Connection Response Code",String.valueOf(response.getStatusLine().getStatusCode())); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } return "Error : " + response .getStatusLine() .getStatusCode() + " " + response .getStatusLine().getReasonPhrase(); } catch (MalformedURLException e) { return e.getMessage(); } catch (UnsupportedEncodingException e) { return e.getMessage(); } catch (IOException e) { return e.getMessage(); } } @Override protected void onPostExecute(String jsonResponse) { //super.onPostExecute(s); Log.i("RECEIVED",jsonResponse); } }
解决方案While I am adding to GSON external library to my project , I just did step which is RightClickProject>Build Path>Configure Build Path(Libraries Tab)->Add External JARs steps and I forgot these instructions place it in the war/WEB-INF/lib folder of your google web project that's why , when I want to receive datas thanks to gson from serverside project , I got the 500 Internal Server Error as a response message. According to this, I suggest everybody who got these problem , be careful while adding any external library to your project.
Link which is help to me to solve problem is LINK .
这篇关于500内部服务器错误GAE试图JSON字符串发送给客户端Android版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!