0

仲間のコーダー、私は eclipse で localhost phpmyadmin データベースを使用してアカウントにログインできる Java コードを書きました。アカ​​ウントが登録されている場合、checklogin は機能しています。クラッシュしました 誰かに助けてもらいたい .. 入力した情報が間違っている場合にやりたいことはすべて、ユーザー名またはパスワードが間違っていることを伝えるメッセージが表示されます 誰かが私を助けてくれることを願っています

これが私のコードです:

public class user extends Activity  implements OnClickListener {
EditText etUser , etPass;
Button bLogin;

String username, password ;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs ;

HttpResponse response;
HttpEntity entity;

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user);

    initialise();
    }

    private void initialise() {
      // TODO Auto-generated method stub
      etUser = (EditText) findViewById(R.id.editText2);
      etPass = (EditText) findViewById(R.id.editText1);
      bLogin = (Button) findViewById(R.id.button1);
      bLogin.setOnClickListener(this);
    }

    public void onregister(final View button) {
    final Intent intent = new Intent();
    intent.setClass(this, register.class);
    startActivity(intent);

    }

    public void onClick(final View v) {

    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("http://10.0.2.2/android/database.php");

    username = etUser.getText().toString();
    password = etPass.getText().toString();

    try {
      nameValuePairs = new ArrayList<NameValuePair>();

      nameValuePairs.add(new BasicNameValuePair("username", username));
      nameValuePairs.add(new BasicNameValuePair("password", password));

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      final Thread a = new Thread(new Runnable() {

        public void run() {
          try {
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

          runOnUiThread(new Runnable() {

            @Override
            public void run() {
              // TODO Auto-generated method stub
              if (response.getStatusLine().getStatusCode() == 200) {

                entity = response.getEntity();

                if (entity != null) {

                  InputStream instream = null;
                try {
                    instream = entity.getContent();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                  JSONObject jsonResponse = null;
                try {
                    jsonResponse = new JSONObject(convertStreamToString(instream));
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                  String retUser = null;
                try {
                    retUser = jsonResponse.getString("username");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  String retPass = null;
                try {
                    retPass = jsonResponse.getString("password");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                  if (username.equals(retUser) && password.equals(retPass)) {

                    SharedPreferences sp = getSharedPreferences("logindetails", 0);

                    SharedPreferences.Editor spedit = sp.edit();

                    spedit.putString("username", username);
                    spedit.putString("password", password);

                    spedit.commit();

                    Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show();

                  } else {

                    Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();

                  }
                }
              }
            }
          });
        }
      });
      a.start();
    } catch (Exception e) {

        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();

    }

    }

    private static String convertStreamToString(final InputStream is) {

    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    final StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (final IOException e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
      } catch (final IOException e) {
        e.printStackTrace();
      }
    }
    return sb.toString();
    }}
4

1 に答える 1

0

ほとんどの場合、間違ったものを提供するUSERNAMEと、行で例外PASSWORDが発生します

response = httpclient.execute(httppost);

「401不正アクセス」のようなもの。だから、それをキャッチしてトーストを見せることができます。また、間違った入力データを提供した場合に備えて、次の作業を HttpResponse で条件にラップできる場合は、いくつかの変数を作成します。

擬似コード:

response = httpclient.execute(httppost); // here you catch error
// and in catch block initialise your variable incorrect = true
if (incorrect) {
   // show Toast
}
else (
  // do your work with JSON
)
于 2013-03-23T15:48:16.653 に答える