Communication between the Android app, JSP server, and DB is not working

If you press the send button on the Android app, no error will appear.

I am studying Android app development for the first time, and I am also studying jsp and db for the first time. I keep trying, but it doesn’t work out.

What should I fix?

help

[APK]

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText name = findViewById(R.id.inputName);
        EditText age = findViewById(R.id.inputAge);

        Button submit = findViewById(R.id.submit);

        View.OnClickListener click = new View.OnClickListener() {
            public void onClick(View view) {
                int id = view.getId();

                if (id == submit.getId()) {
                    OkHttpClient client = new OkHttpClient();

                    String url = "http://adress.../write.jsp?name=" + name.getText().toString() + "&age=" + age.getText().toString();

                    Request request = new Request.Builder()
                            .url(url)
                            .get()
                            .build();

                    client.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {
                                final String myResponse = response.body().string();
                            }
                        }
                    });
                }
            }
        };
        submit.setOnClickListener(click);
    }
}

[write.jsp]

<%
        Class.forName("org.mariadb.jdbc.Driver");
        try ( Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/testdb", "test", "1>
              Statement writeDB = conn.createStatement();)
        {
                request.setCharacterEncoding("UTF-8");
                String inputName = request.getParameter("name");
                int inputAge = Integer.parseInt(request.getParameter("age"));
                String sql = String.format("insert into test (name, age) values ('%s', '%d')",inputName, input>
                writeDB.executeUpdate(sql);
                out.println("susses");
        } catch (Exception e) {
            out.println("error");
        }

%>

  • There is no error, but it cannot be saved to the server.

    – 

  • The first problem occurred because the port was not written in the address. This was resolved, but error code 500 appears. trying to solve

    – 

  • If you press it once, a 500 error code will appear. However, if you press it twice in succession, it is saved to the db normally. I don’t know what the problem is.

    – 

  • HTTP 500 means server side error so check the server logs of the server serving the JSP. BTW: you should better edit your question instead of adding multiple comments.

    – 




Leave a Comment