gpt4 book ai didi

java - 当我输入错误的用户名或密码时,它只是说**请输入用户名和密码**

转载 作者:行者123 更新时间:2023-12-02 06:26:39 25 4
gpt4 key购买 nike

这是我的 Login.java

public class Login extends ActionBarActivity implements OnClickListener  {

private Button login,register;
private EditText email,password;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL = "http://192.168.1.14:1234/PMSS/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.registerlauncher);
email = (EditText) findViewById(R.id.userid);
password = (EditText) findViewById(R.id.password);


login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Username = email.getText().toString();
String Password = password.getText().toString();
new AttemptLogin(Username,Password).execute();
}
});

register.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);

}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(Login.this, Register.class);
startActivity(i);
break;
*/
default:
break;
}
}

class AttemptLogin extends AsyncTask<String, String, Integer> {

boolean failure = false;
String res;
String Username;
String Password;
int success;
public AttemptLogin(String Username, String Password) {
this.Username = Username;
this.Password = Password;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();

}

protected Integer doInBackground(String... args) {

try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));

Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
System.out.print("Here");
// check your log for json response
Log.d("Login attempt", json.toString());

// json success tag
success = json.getInt(TAG_SUCCESS);
res = json.getString(TAG_MESSAGE);

return success;
} catch (JSONException e) {
e.printStackTrace();
}

return null;

}

protected void onPostExecute(Integer success) {
// dismiss the dialog once product deleted
pDialog.dismiss();

if (success != null && success == 1) {
Log.d("Login Successful!", "res: " + res);
Intent i = new Intent(Login.this, MainMenu.class);
startActivity(i);
Toast.makeText(Login.this, res == null? "Please enter user id and password" : res, Toast.LENGTH_LONG).show();
email.setText(null);
password.setText(null);
}else{
Log.d("Login Failure!", "res: " + res);
Toast.makeText(Login.this, res == null? "Please enter user id and password" : res, Toast.LENGTH_LONG).show();
}

}
}
}

这是我的日志猫

12-08 17:12:28.559: D/request!(8709): starting
12-08 17:12:29.109: E/JSON Parser(8709): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
12-08 17:12:29.109: D/Login attempt(8709): {"message":"Please fill in the login details!"}
12-08 17:12:29.119: W/System.err(8709): org.json.JSONException: No value for success
12-08 17:12:29.119: W/System.err(8709): at org.json.JSONObject.get(JSONObject.java:354)
12-08 17:12:29.119: W/System.err(8709): at org.json.JSONObject.getInt(JSONObject.java:443)
12-08 17:12:29.119: W/System.err(8709): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:143)
12-08 17:12:29.119: W/System.err(8709): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-08 17:12:29.119: W/System.err(8709): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-08 17:12:29.119: W/System.err(8709): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-08 17:12:29.119: W/System.err(8709): at java.lang.Thread.run(Thread.java:1019)
12-08 17:12:29.129: D/Login Failure!(8709): res: null

第 143 行:success = json.getInt(TAG_SUCCESS);

为什么我输入了错误的用户名或密码,但它并没有说您输入了错误的用户名或密码。还是我没有编写错误处理的java代码?我真的很感谢你们的帮助..

最佳答案

当您输入错误的凭据时,您会收到此响应 -

{"message":"请填写登录详细信息!"}

它们在响应中没有“成功”键,但在代码中您正在解析它的值,这就是它给出 JSONExpection 的原因。

关于java - 当我输入错误的用户名或密码时,它只是说**请输入用户名和密码**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com