[英]Regarding android server error 500
我正在嘗試向我在玻璃魚服務器中部署的Web應用程序servlet發送請求,並使用volley HTTP庫接收json響應。 我設法從幾個servlet獲得響應,但是有一個servlet我得到錯誤BasicNetwork.performRequest:意外的響應代碼500 。 我嘗試切換HTTP方法,在servlet中將內容類型設置為“application / json”並實現getHeaders()
方法,並且可以在SO中給出關於此問題的解決方案。 但我還是無法解決這個問題。
我的排球請求如下:
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
我是android的新手。 任何幫助都會很明顯。 謝謝。
更新:
以下是我使用HttpRequester訪問url的方法:
基於
JSONException:Value
<!DOCTYPE
您的servlet返回HTML,而不是純文本JSON,因此無法解析。
由於您收到500內部服務器錯誤,因此HTML內容是真正的錯誤。 如果您從瀏覽器訪問該URL,或使用Postman等工具,您將能夠看到servlet的堆棧跟蹤。
解決之后,您可能希望在請求或響應中將Content-Type的頭設置為application / json以強制寫入json。
有關解決方案的完整詳細信息,請參閱如何使用Java發送JSON
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.