gpt4 book ai didi

java - 尝试将 JSON 字符串发送到 Android 客户端时出现 500 内部服务器错误 GAE?

转载 作者:行者123 更新时间:2023-12-01 11:35:41 27 4
gpt4 key购买 nike

我正在使用 Google App Engine 来存储数据。我的应用程序是一个 Android 应用程序,我想从 GAE 数据存储端接收数据。但是,当我想接收 JSON 文本形式的 ArrayList 时,我收到“错误:500”内部服务器错误”消息,我找不到任何解决方案,我将客户端和服务器端代码放在这里;

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);
}
}

最佳答案

当我将 GSON 外部库添加到我的项目时,我刚刚执行了 RightClickProject>构建路径>配置构建路径(库选项卡)->添加外部 JAR 步骤,但我忘记了这些说明 将其放在 google web 项目的 war/WEB-INF/lib 文件夹中 这就是为什么,当我想从服务器端项目接收 gson 的数据时,我得到了 500 内部服务器错误作为响应消息。据此,我建议遇到这些问题的每个人在向项目添加任何外部库时要小心。

帮助我解决问题的链接是 LINK

关于java - 尝试将 JSON 字符串发送到 Android 客户端时出现 500 内部服务器错误 GAE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com