这是我的剧本:
public class LoginActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
session = new SessionManager(getApplicationContext());
if (session.isLoggedIn()) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (email.trim().length() > 0 && password.trim().length() > 0) {
checkLogin(email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
private void checkLogin(final String email, final String password) {
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
session.setLogin(true);
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "login");
params.put("idm", email);
params.put("pwd", password);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
&#13;
并且logcat显示:
org.json.JSONException:类型为java.lang.Integer时出错的值为1 无法转换为布尔值
我使用此脚本使用我自己的数据库创建登录页面。这个脚本使用电子邮件作为用户名,但在我的数据库中,我使用数字作为用户名。
答案 0 :(得分:1)
你的问题在于:
boolean error = jObj.getBoolean("error");
如果你的json对象中的“error”不是“true”或“false”,你将得到异常:
java.lang.Integer类型的错误值1无法转换为 布尔值。
而是使用
int intError = jObj.getInt("error");
boolean error = (intError > 0) ? true : false;
答案 1 :(得分:-1)
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
您的回复&#34;错误&#34; object不是布尔值,因此您可以根据exception
消息将其转换为String或Integer。如下所示
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getString("error");
// or
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getInteger("error");