【问题标题】:Regarding android server error 500关于android服务器错误500
【发布时间】:2016-03-02 13:33:18
【问题描述】:

我正在尝试向部署在 glass fish 服务器中的 Web 应用程序 servlet 发送请求,并使用 volley HTTP 库接收 json 响应。我设法从几个 servlet 获得响应,但是使用一个 servlet 我收到错误 BasicNetwork.performRequest: Unexpected response code 500 。我尝试切换 HTTP 方法,在 servlet 中将内容类型设置为“application/json”并实现getHeaders() 方法,并可能在 SO 中给出关于此问题的解决方案。但我还不能解决这个问题。

我的 Volley 请求如下:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i("RESPONSE",response.toString());
                //Type type = new TypeToken<CartItem>() {}.getType();
                //CartItem item = new Gson().fromJson(response.toString(), type);
                //cartItem = item;
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.i("Volley_error", error.toString());
    }
});

我在浏览器中的 json 响应如下所示:

{
    "items": [{
        "p_id": 1,
        "p_name": "Multi Checked Shirt",
        "p_price": 1234.0,
        "p_size": "M",
        "p_img": "uploads/products/Dorothy-Perkins-Multi-Checked-Shirt-4510-9254471-1-pdp_slider_l.jpg",
        "p_qnty": 1
    }, {
        "p_id": 15,
        "p_name": "Rust Solid Coloured Pant",
        "p_price": 3500.0,
        "p_size": "S",
        "p_img": "uploads/products/Dorothy-Perkins-Rust-Solid-Coloured-Pant-971120977.jpg",
        "p_qnty": 2
    }],
    "cart_total": 8234.0,
    "total_items": 3
}

我的 Servlet 代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getSession(false).getAttribute("cart") != null) {

        ShoppingCart cart = (ShoppingCart) request.getSession(false).getAttribute("cart");
        List<CartItem> cartItems = cart.getShoppingList();
        List<Double> tot = new ArrayList<>();
        CartModel model = new CartModel();
        List<CartItemModel> cartItemModels = new ArrayList<>();
        for (CartItem item : cartItems) {

            CartItemModel itemModel = new CartItemModel(item.getProduct_id(),
                    cart.getNameofProduct(item.getProduct_id()),
                    cart.getPriceofProduct(item.getProduct_id(), item.getSize()),
                    item.getSize(),
                    path + cart.getImageofProduct(item.getProduct_id()),
                    item.getQnty());
            cartItemModels.add(itemModel);
            tot.add(cart.getPriceofProduct(item.getProduct_id(), item.getSize()) * item.getQnty());

        }
        model.setCart_total(cart.grandTotal(tot));
        model.setTotal_items(cart.getTotalItemsOfTheCart());
        model.setItems(cartItemModels);
        Type type = new TypeToken<CartModel>() {
        }.getType();
        String element = new Gson().toJson(model, type);
        response.getWriter().write(element);
    }
}

我知道修改 Volley 库代码不是一个好主意,但我也尝试过。

if (statusCode < 200 || statusCode > 299) { throw new IOException();}

if (statusCode < 200 || statusCode > 505) {throw new IOException();}

然后我得到了错误:

JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject

我是安卓新手。任何帮助都是不言而喻的。谢谢。

更新:

以下是我使用 HttpRequester 访问 url 时的方式:

【问题讨论】:

  • 发出请求时 glassfish 错误日志中是否有异常?
  • 不...服务器中没有任何错误....
  • 服务器内部错误,所以服务器应该有错误
  • 这就是问题......服务器中没有任何错误......一旦我运行servlet,它就会给我上面提到的json输出。

标签: android json android-volley internal-server-error


【解决方案1】:

基于

JSONException:值&lt;!DOCTYPE

您的 servlet 返回 HTML,而不是纯文本 JSON,因此无法解析。

由于您收到 500 内部服务器错误,因此 HTML 内容是 真正的 错误。如果您从浏览器访问该 URL,或使用 Postman 等工具,您将能够看到您的 servlet 的堆栈跟踪。

修复该问题后,您可能希望在请求或响应中将 Content-Type 的标头设置为 application/json 以强制写入 json。

有关解决方案的完整详细信息,请参阅How to send JSON back with Java

【讨论】:

  • 您也可以使用 volley StringRequest 直到您确信 URL 将始终返回 JSON
  • 我把 contentType("application/json") 放在我的 servlet 中。还是一样的错误。 BasicNetwork.performRequest: Unexpected response code 500.
  • 正如我一直想说的,你找错地方了。问题出在 Android 上 Servlet 上
  • 但正如我在问题中提到的那样,我正在从几个 servlet 获取 json 数据到我的 android 应用程序。那些工作正常而不是这个servlet。我在这些 servlet 中也做过同样的事情。
  • 您在显示的 Android 代码和放置在显示的 HttpRequester 图像中的代码之间是否使用相同的 URL?根据您的图片,我可以看到 that servlet 工作并返回 200 OK 响应。不管url你的android代码调用返回一个500内部服务器错误,虽然
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
相关资源
最近更新 更多