簡體   English   中英

在Eclipse Android開發中更正Java代碼

[英]correct a java code in eclipse android development

各位研究員,我在eclipse中寫了一個Java代碼,可以使用localhost phpmyadmin數據庫登錄到一個帳戶,checklogin正在運行,如果該帳戶已注冊,則可以登錄,但是如果我輸入了錯誤的android模擬器,則告訴我該應用程序具有崩潰了,我希望有人幫助我..如果鍵入的信息錯誤,我想做的所有事情都會顯示一條消息,提示用戶名或密碼錯誤,希望有人可以幫助我

這是我的代碼:

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();
    }}

如果您提供的USERNAMEPASSWORD不正確,很可能會在第一行獲得Exception

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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM