1

Im using Restful Java to parse data json with POST method and Volley in android. When I try to insert data from Advanced REST Client, it insert data into db success, but in Android, when I insert data and see that it happen an exception as below:

My Restful (UPDATED):

@Path("/bookticket")
public class BookServiceCalled {
@POST 
@Path("/json")      
@Produces("application/json;charset=utf-8")
@Consumes("application/x-www-form-urlencoded")  //   MediaType.APPLICATION_JSON
public static void bookFromUser(@QueryParam("fullname") String fullname, 
                                 @QueryParam("birthday") Date birthday, 
                                 @QueryParam("address") String address) throws Exception {
    Connection connection = null;
    PreparedStatement statement = null;     
    try {
        // Config connect mydb          
        String query = "INSERT INTO order "
                + "(fullname, birthday, address) VALUES "
                + "(?,?,?) ";           
        statement = (PreparedStatement) connection.prepareStatement(query);         
        statement.setString(1, fullname);
        statement.setDate(2, birthday);
        statement.setString(3, address);

        statement.executeUpdate();          

        System.out.println("Work !");
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        if (connection != null) {
            connection.close();
        }
            throw e;
    } 
    finally {
            if (connection != null) {
                connection.close();
        }
    }       
}        

}

My activity class:

    private static final String URL_BOOK = "http://192.168.1.221:9999/bookticket_service/bookticket/json";

    EditText fullname, birthday, address;

    fullname = (EditText) findViewById(R.id.fullname);
    birthday = (EditText) findViewById(R.id.birthday);
    address = (EditText) findViewById(R.id.address);
    btnBook = (Button) findViewById(R.id.btnBook);
    requestQueue = Volley.newRequestQueue(getApplicationContext());

    btnBook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            StringRequest request = new StringRequest(Request.Method.POST, URL_BOOK, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    System.out.println(response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Error: " + error.getMessage());

                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_LONG).show();                        
                }
            }){
               @Override
                protected Map<String,String> getParams() throws AuthFailureError {
                   Map<String, String> parameter = new HashMap<String, String>();
                   parameter.put("Content-Type", "application/json; charset=utf-8");
                   parameter.put("fullname", fullname.getText().toString());
                   parameter.put("birthday", birthday.getText().toString());
                   parameter.put("address", address.getText().toString());
                   return parameter;
               }
            };
            // volley saves the response in its cache and this behavior may cause some strange problem
            // request.setShouldCache(false);
            requestQueue.add(request);
        }
    });

Logcat:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Column 'fullname' cannot be null

How to fix the exception, thanks for the help

Khanh Luong Van
  • 476
  • 3
  • 14
  • 31

0 Answers0