如何在android中使用onPostExecute

时间:2016-11-05 08:09:18

标签: php android android-studio android-studio-2.2

我想将值从我的php文件传递到android studio,所以我可以让用户或管理员登录到不同的页面,但是我应该如何以及在哪里将onPostExecute放入我的代码?我希望有人可以教我因为我是新手在android中。 T.T感到困难

public class LoginActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);



    final EditText etUsername = (EditText) findViewById(R.id.etUsername);
    final EditText etPassword = (EditText) findViewById(R.id.etPassword);

    final Button bLogin = (Button) findViewById(R.id.bLogin);
    final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);

    registerLink.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
            LoginActivity.this.startActivity(registerIntent);
        }
    });
    bLogin.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            final String username = etUsername.getText().toString();
            final String password = etPassword.getText().toString();

           Response.Listener<String> responseListener = new Response.Listener<String>(){

                @Override
                public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");


                            if (etUsername.length() <= 1 || etPassword.length() <= 1) {
                                Toast.makeText(LoginActivity.this, "Please fill in Username and Password!", Toast.LENGTH_LONG).show();
                                return;
                            } else if (success) {


                                String name = jsonResponse.getString("name");

                                Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);

                                LoginActivity.this.startActivity(intent);

                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage("Login Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
            };
            LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
            queue.add(loginRequest);
        }
    });
}
}

0 个答案:

没有答案