簡體   English   中英

500內部服務器錯誤GAE,同時嘗試將JSON字符串發送到客戶端android端?

[英]500 Internal Server Error GAE while trying to send JSON string to client android side?

我正在Google App Engine上存儲數據。我的應用程序是android應用程序,我想從GAE數據存儲端接收數據。但是,當我想以JSON文本形式接收ArrayList時,出現“錯誤:500 Internal Server Error “消息,我找不到我的客戶端和服務器端代碼放在這里的解決方案;

服務器端

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

客戶端

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> Build Path> Configure Build Path(Libraries Tab)-> Add External JARs步驟,我忘記了這些說明將其放置在war / WEB-INF中這就是您的Google Web項目的/ lib文件夾的原因,當由於服務器端項目中的gson而要接收數據時,我收到500 Internal Server Error作為響應消息。 據此,我建議遇到這些問題的所有人,在向您的項目添加任何外部庫時要小心。

對我有幫助解決問題的鏈接LINK

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM