排球:[213] BasicNetwork.performRequest:意外的响应代码415

时间:2017-02-08 10:57:05

标签: android json rest android-volley

我使用Restful Java用POST方法解析数据json,在android中解析Volley。当我尝试从Advanced REST Client插入数据时,它会将数据插入到db success中,但在Android中,当我插入数据并看到它发生异常时如下所示:

My Restful(更新):

@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();
        }
    }       
}        

}

我的活动课程:

    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

如何修复异常,感谢您的帮助

0 个答案:

没有答案