我正在制作一个Android应用程序,它包含一个使用SMS验证的简单登录。使用Spring MVC进行Web服务。
这是我的android代码:
package net.simplifiedcoding.androidotp;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//Creating views
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextPhone;
private EditText editTextConfirmOtp;
private AppCompatButton buttonRegister;
private AppCompatButton buttonConfirm;
//Volley RequestQueue
private RequestQueue requestQueue;
//String variables to hold username password and phone
private String username;
private String password;
private String phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing Views
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextPhone = (EditText) findViewById(R.id.editTextPhone);
buttonRegister = (AppCompatButton) findViewById(R.id.buttonRegister);
//Initializing the RequestQueue
requestQueue = Volley.newRequestQueue(this);
//Adding a listener to button
buttonRegister.setOnClickListener(this);
}
//This method would confirm the otp
private void confirmOtp() throws JSONException {
//Creating a LayoutInflater object for the dialog box
LayoutInflater li = LayoutInflater.from(this);
//Creating a view to get the dialog box
View confirmDialog = li.inflate(R.layout.dialog_confirm, null);
//Initizliaing confirm button fo dialog box and edittext of dialog box
buttonConfirm = (AppCompatButton) confirmDialog.findViewById(R.id.buttonConfirm);
editTextConfirmOtp = (EditText) confirmDialog.findViewById(R.id.editTextOtp);
//Creating an alertdialog builder
AlertDialog.Builder alert = new AlertDialog.Builder(this);
//Adding our dialog box to the view of alert dialog
alert.setView(confirmDialog);
//Creating an alert dialog
final AlertDialog alertDialog = alert.create();
//Displaying the alert dialog
alertDialog.show();
//On the click of the confirm button from alert dialog
buttonConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Hiding the alert dialog
alertDialog.dismiss();
//Displaying a progressbar
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, "Authenticating", "Please wait while we check the entered code", false, false);
//Getting the user entered otp from edittext
final String otp = editTextConfirmOtp.getText().toString().trim();
//Creating an string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CONFIRM_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//if the server response is success
if(response.equalsIgnoreCase("success")){
//dismissing the progressbar
loading.dismiss();
//Starting a new activity
startActivity(new Intent(MainActivity.this, Success.class));
}else{
//Displaying a toast if the otp entered is wrong
Toast.makeText(MainActivity.this,"Wrong OTP Please Try Again",Toast.LENGTH_LONG).show();
try {
//Asking user to enter otp again
confirmOtp();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
alertDialog.dismiss();
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
//Adding the parameters otp and username
params.put(Config.KEY_OTP, otp);
params.put(Config.KEY_USERNAME, username);
return params;
}
};
//Adding the request to the queue
requestQueue.add(stringRequest);
}
});
}
//this method will register the user
private void register() {
//Displaying a progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Registering", "Please wait...", false, false);
System.out.print("sdasdasdasd");
//Getting user data
username = editTextUsername.getText().toString().trim();
password = editTextPassword.getText().toString().trim();
phone = editTextPhone.getText().toString().trim();
System.out.print(phone);
//Again creating the string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
try {
System.out.print("RESPONSE "+response);
//Creating the json object from the response
JSONObject jsonResponse = new JSONObject(response);
//If it is success
if(jsonResponse.getString(Config.TAG_RESPONSE).equalsIgnoreCase("Success")){
//Asking user to confirm otp
System.out.print("monelGUPTALEMPMNNAsd\n");
confirmOtp();
}else{
//If not successful user may already have registered
Toast.makeText(MainActivity.this, "Username or Phone number already registered", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Toast.makeText(MainActivity.this, error.getMessage(),Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> paramss = new HashMap<String, String>();
paramss.put("Content-Type","application/json");
return paramss;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//Adding the parameters to the request
params.put(Config.KEY_USERNAME, username);
params.put(Config.KEY_PASSWORD, password);
params.put(Config.KEY_PHONE, phone);
return params;
}
};
//Adding request the the queue
requestQueue.add(stringRequest);
}
@Override
public void onClick(View v) {
//Calling register method on register button click
register();
}
}
这是我得到的以下错误:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:208) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:197) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:147) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [catalina.jar:8.0.26]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.26]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.26]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.26]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.26]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) [catalina.jar:8.0.26]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.26]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:8.0.26]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [catalina.jar:8.0.26]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.26]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) [catalina.jar:8.0.26]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.26]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) [catalina.jar:8.0.26]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) [tomcat-coyote.jar:8.0.26]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673) [tomcat-coyote.jar:8.0.26]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526) [tomcat-coyote.jar:8.0.26]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482) [tomcat-coyote.jar:8.0.26]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_60]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.26]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1487) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:518) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3323) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2482) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:801) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:697) ~[jackson-core-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3602) ~[jackson-databind-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3547) ~[jackson-databind-2.5.3.jar:2.5.3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2662) ~[jackson-databind-2.5.3.jar:2.5.3]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:221) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 39 common frames omitted
19:47:46.947 [http-nio-8080-exec-28] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void AnotherMVC.com.snapdeal.bull.MainController1.Register(AnotherMVC.com.snapdeal.bull.User)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
19:47:46.947 [http-nio-8080-exec-28] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void AnotherMVC.com.snapdeal.bull.MainController1.Register(AnotherMVC.com.snapdeal.bull.User)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
19:47:46.947 [http-nio-8080-exec-28] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public void AnotherMVC.com.snapdeal.bull.MainController1.Register(AnotherMVC.com.snapdeal.bull.User)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
19:47:46.947 [http-nio-8080-exec-28] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public void AnotherMVC.com.snapdeal.bull.MainController1.Register(AnotherMVC.com.snapdeal.bull.User)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
19:47:46.947 [http-nio-8080-exec-28] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'phone': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@8dfb02f; line: 1, column: 7]
1
错误显示手机是布尔值而不是字符串,但我检查了它的布尔值
这是我的配置文件:
package net.simplifiedcoding.androidotp;
public class Config {
//URLs to register.php and confirm.php file
public static final String REGISTER_URL = "http://10.0.2.2:8080/FinalSmsApi/rest/requestSms/hello";
public static final String CONFIRM_URL = "http://10.0.2.2:8080/FinalSmsApi/rest/confirm";
//Keys to send username, password, phone and otp
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_PHONE = "phone";
public static final String KEY_OTP = "otp";
//JSON Tag from response from server
public static final String TAG_RESPONSE= "ErrorMessage";
}