1

I'm trying to send my mysql data from servlet using JSON to android app but it shows the error java.net.connectException connection timed out

This is debugging image

This is android Fragment with recylcer View. RecyclerView works fine when data is dummy

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_homefrag, container, false);
        final Context context=v.getContext();
        recyclerView= v.findViewById(R.id.recyler);
        final List<UserRecycle> sample=new ArrayList<>();
final String JSON_URL = "http://10.0.2.2:8080/OnlineBookStore/Home";
        RequestQueue requestQueue= Volley.newRequestQueue(context);
        StringRequest stringRequest=new StringRequest(Request.Method.GET, JSON_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try{
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray bookArray=jsonObject.getJSONArray("BookData");

                    for(int i=0;i<bookArray.length();i++){
                        JSONObject bookobject=bookArray.getJSONObject(i);

                        UserRecycle books=new UserRecycle();
                        book_ids.add(bookobject.getString("id"));
                        books.b_name=bookobject.getString("bname");
                        books.b_author=bookobject.getString("bauthor");
                        books.b_price=bookobject.getString("bprice");
                        books.b_avail=bookobject.getString("bstock");
                        books.b_image=R.drawable.bbanim;
                        sample.add(books);

                    }

                    layoutManager=new LinearLayoutManager(getContext());
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setHasFixedSize(true);
                    recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
                    recyclerView.setAdapter(new RecyclerAdapter(sample));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(getContext(), "error "+error.getMessage(), Toast.LENGTH_LONG).show();

            }
        });

        stringRequest.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(stringRequest);

        return v;
    }

This is My Servlet Class

public class Home extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection con=Connect.connect();
        try{
             PrintWriter out=response.getWriter();
        PreparedStatement pst=con.prepareStatement("select * from book order by b_id desc;");
        ResultSet rst=pst.executeQuery();


            JsonObject jsonResponse= new JsonObject();            
            JsonArray data = new JsonArray();

            while(rst.next()){

                JsonObject row=new JsonObject();
                row.addProperty("id", rst.getString(1));
                row.addProperty("bname", rst.getString(2));
                row.addProperty("bauthor", rst.getString(3));
                row.addProperty("bprice", rst.getString(4));
                row.addProperty("bstock", rst.getString(5));
                data.add(row);
            }
            jsonResponse.add("BookData", data);
            out.print(jsonResponse);
        }
        catch(SQLException e){
        }
    }
}

When I print the data in a web page it works fine.

I guess the problem is with either creating response in Servlet class or the url. Please Help if you can thanks.

Deepak Rawat
  • 37
  • 1
  • 6
  • You have `Request.Method.GET` in android and servlet have code in `processRequest` ,I think you need to put your code in `doGet` method because your client is sending `get` request to server. Also, [check](https://stackoverflow.com/a/5663054/10606400) this. – Swati Sep 10 '19 at 13:07
  • Now I'm getting java.net.socketException Connection reset – Deepak Rawat Sep 10 '19 at 15:17

0 Answers0