I am trying to send request to my my web application servlet deployed in glass fish sever and receive json response using volley HTTP library. I managed to get the response from several servlets but but with one servlet I m getting error BasicNetwork.performRequest: Unexpected response code 500 . I tried with switching HTTP methods, setting content type as "application/json" in servlet and implement the getHeaders()
method and may solutions given in SO regarding this issue. But I couldnt solve this problem yet.
My Volley request is given below :
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());
}
});
My json response in the browser looks like below :
{
"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
}
My Servlet code :
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);
}
}
I know it is not a good idea to modify the Volley library code but I tried that too.
if (statusCode < 200 || statusCode > 299) { throw new IOException();}
to
if (statusCode < 200 || statusCode > 505) {throw new IOException();}
Then I got Error :
JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject
I am new to android . Any help would be appreciable. Thank you.
UPDATE :
Here is how when I access url with HttpRequester :
Based on
JSONException: Value
<!DOCTYPE
Your servlet is returning HTML, not plaintext JSON, so it can't be parsed.
Since you are getting a 500 Internal server error, that HTML content is the real error here. If you access that URL from your browser, or using a tool such as Postman, you'll be able to see the stacktrace of your servlet.
Once you fix that, you may want to set the header for Content-Type to application/json in either the request or response to force json to be written.
For full details of a solution, see How to send JSON back with Java
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.