在将JSON对象发送到我的服务器期间,我正在努力解决错误500。我正在使用POST方法,因为我需要将用户保存在数据库中。我想我的请求中有些错误导致输出不打印(在onResponse方法中)。在服务器端,数据也不会打印,但没有处理异常,没有错误 - 一切正常但数据仍然不存在!
Android注册方法:
public class RegisterIndividualActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_individual);
setListeners();
}
private void setListeners() {
final EditText loginField = (EditText) findViewById(R.id.loginField);
final EditText passwordField = (EditText) findViewById(R.id.passwordField);
Button registerButton = (Button) findViewById(R.id.registerButton);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register(
loginField.getText(),
passwordField.getText(),
);
}
});
}
private void register(final Editable login,
final Editable password){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Creating new acc...");
progressDialog.show();
RequestQueue queueAcc = Volley.newRequestQueue(this);
String urlAcc = getResources().getString(R.string.server_address)+"android/user/register?login="
+ login.toString() + "&password=" + password.toString();
JSONObject newUser = new JSONObject();
try {
newUser.put("login", login.toString());
newUser.put("password", password.toString());
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("USER TO SAVE: " + newUser.toString());
JsonObjectRequest urlAccStringRequest = new JsonObjectRequest(Request.Method.POST, urlAcc, newUser, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("Good morning " + login.toString()); //doesnt print
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { //i got this error
progressDialog.dismiss();
Toast.makeText(RegisterIndividualActivity.this, "Connection error", Toast.LENGTH_SHORT).show();
}
});
queueAcc.add(urlAccStringRequest);
queueAcc.start();
}
}
服务器端的控制器(这是适当的类,带有@Controller注释,需要映射,即android / user):
@RequestMapping(value = "/register", method = RequestMethod.POST)
public void register(
@RequestParam("login") String login,
@RequestParam("password") String password,
HttpServletRequest request, HttpServletResponse response, Model model
){
System.out.println("\nIn Register method..");
UserModel uM = new UserModel();
HttpSession session = request.getSession(true);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
uM.setAccountType("Client");
JsonParser parser = new JsonParser();
try {
System.out.println("\nTrying to save..");
JsonObject jsonObject = (JsonObject) parser.parse(request.getReader());
System.out.println(jsonObject.get("login").toString()); //event this doesnt print
uM.setLogin(jsonObject.get("login").toString());
uM.setPassword(jsonObject.get("password").toString());
System.out.println(uM.toString()); //doesnt print
userDAO.save(uM);
} catch (IOException e) { // doesnt throw exception
e.printStackTrace();
}
System.out.println(uM.toString()); // doesnt print
}
@EDIT 添加了服务器日志:
19-Jun-2017 19:42:22.757 SEVERE [http-nio-8080-exec-1] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [spring-mvc] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to javax.json.JsonObject] with root cause
java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to javax.json.JsonObject
at com.mvc.controller.AndroidUserController.register(AndroidUserController.java:145)
at com.mvc.controller.AndroidUserController$$FastClassBySpringCGLIB$$448b2d05.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.mvc.controller.AndroidUserController$$EnhancerBySpringCGLIB$$47d3305.register(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
@EDIT 2
这是将try-catch块更改为此后的服务器日志:
try {
System.out.println("\nTrying to save..");
JsonObject jsonObject = (JsonObject) parser.parse(request.getReader());
System.out.println(jsonObject.get("login").toString()); //event this doesnt print
} catch (IOException e) { // doesnt throw exception
e.printStackTrace();
}
服务器日志:
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
@EDIT 3 !!!!
我发现这一行正在调整第二个服务器日志中显示的错误:
JsonObject jsonObject = (JsonObject) parser.parse(request.getReader());
这有什么问题?
@EDIT 4
这是我目前阅读json的代码:
try {
System.out.println("\nTrying to save...");
InputStream inputStream = request.getInputStream();
JsonReader reader = Json.createReader(inputStream); //this is were error is
reader.close();
// JsonObject jsonObject = reader.readObject();
// reader.close();
// System.out.println(jsonObject.get("login").toString());
//// uM.setLogin(jsonObject.get("login").toString());
// uM.setPassword(jsonObject.get("password").toString());
// System.out.println(uM.toString());
// userDAO.save(uM);
} catch (IOException e) {
e.printStackTrace();
}
这是当前的服务器日志:
19-Jun-2017 21:36:34.490 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
19-Jun-2017 21:36:34.617 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
19-Jun-2017 21:36:46.631 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring FrameworkServlet 'spring-mvc'
19-Jun-2017 21:36:57.007 SEVERE [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [spring-mvc] in context with path [] threw exception [Request processing failed; nested exception is javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found] with root cause
java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javax.json.spi.JsonProvider.provider(JsonProvider.java:94)
at javax.json.Json.createReader(Json.java:220)
at com.mvc.controller.AndroidUserController.register(AndroidUserController.java:146)
at com.mvc.controller.AndroidUserController$$FastClassBySpringCGLIB$$448b2d05.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at com.mvc.controller.AndroidUserController$$EnhancerBySpringCGLIB$$f920f9fc_2.register(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
com.google.gson.JsonObject cannot be cast to javax.json.JsonObject
您似乎导入了错误的JsonObject类
如果您使用的是Gson,则不是how to parse JSON with it
JsonObject jsonObject = (JsonObject) parser.parse(request.getReader());