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

标签 java android

这是我的 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/

相关文章:

javascript - 在 Android 手机中关闭 Chrome 的建议栏

android - Phonegap/Cordova 应用程序 - 主应用程序 - 不同的实现

java - 缺少名称,处于状态 : START_OBJECT parsing XML using Jackson

java - 如何展平包含列表的对象列表并将每个元素传递到方法中?

java - 媒体播放器问题

java - 在 Android 中,为什么不应该将 Bitmap 放入 Application 类中?

c# - Xamarin.forms 中的动画 gif

java - 蓝牙低功耗葡萄糖GATT配置文件测量解析值

java - 检查字段是否为空

java - 部署数据库时出错