Android登录验证无效

时间:2014-01-31 09:46:52

标签: android http

我有登录申请。当用户填写表单并单击登录按钮时,它从服务器获取一个字符串并检查,如果正确,则转到下一个活动。一切都是正确的,但第一次它不能存储字符串。我认为这是为了后台任务。后台任务的值是字符串。所以if条件取决于该字符串值。在进入下一个任务之前,我怎样才能获得字符串。

这是我的活动:

public class LoginActivity extends Activity implements OnClickListener {

Button login;
EditText user, pass;
CheckBox ck;
String FILENAME = "check";
String checkenords;

String FILETOKEN = "token";
String tokenStr;

String responseStr;
String usernamefromuser;
int responsecode;
String passfromuser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.sign_in);
    user = (EditText) findViewById(R.id.editText1);
    pass = (EditText) findViewById(R.id.editText2);
    ck = (CheckBox) findViewById(R.id.checkBox1);
    login = (Button) findViewById(R.id.btnlogin);

    login.setOnClickListener(this);
}

class CreateUser extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        usernamefromuser = user.getText().toString();
        passfromuser = pass.getText().toString();

        Log.e("successss", "888888888888");
        Log.e("Username", "User:" + usernamefromuser);
        Log.e("Password", "pass:" + passfromuser);

        HttpClient client = new DefaultHttpClient();

        String url = "http://54.228.149.123/api/auth";
        HttpPost httppost = new HttpPost(url);
        // httppost.setHeader("Content-type", "application/json");
        // httppost.setHeader("Accept", "application/json");

        try {
            List<NameValuePair> namevalpair = new ArrayList<NameValuePair>();
            namevalpair.add(new BasicNameValuePair("pass", passfromuser));
            namevalpair.add(new BasicNameValuePair("email",
                    usernamefromuser));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
                    namevalpair, HTTP.UTF_8);
            httppost.setEntity(entity);
            HttpResponse httpresponse = client.execute(httppost);
            responsecode = httpresponse.getStatusLine().getStatusCode();
            responseStr = EntityUtils.toString(httpresponse.getEntity());
            Log.d("Authentication", "" + responsecode);
            // Log.d("httpresponseeeee", httpresponse.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    new CreateUser().execute();
    if (responsecode == 200) {
        tokenStr = responseStr;
        try {
            FileOutputStream fos = openFileOutput(FILETOKEN,
                    Context.MODE_PRIVATE);
            fos.write(tokenStr.getBytes());
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (ck.isChecked()) {
            checkenords = "enable";
            try {
                FileOutputStream fos = openFileOutput(FILENAME,
                        Context.MODE_PRIVATE);
                fos.write(checkenords.getBytes());
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent inteGps = new Intent(LoginActivity.this, Gps.class);
            startActivity(inteGps);
            finish();
        } else {
            Intent inteGps = new Intent(LoginActivity.this, Gps.class);
            startActivity(inteGps);
            finish();
        }
    } else if (responsecode == 401) {

        Toast.makeText(this, "Invalid Username or Password",
                Toast.LENGTH_LONG).show();

    } else{
        Toast.makeText(this, "Please try again", Toast.LENGTH_LONG).show();
    }
}
 }

但是第一次,它显示“请再试一次”,尽管responsecode = 200或responsecode = 401。请帮帮我。

1 个答案:

答案 0 :(得分:0)

响应验证应在AsyncTask的onPostExecute方法中不在onClick本身,因为如果你保持onclick它将立即执行新的CreateUser()。execute( );即它永远不会等待从服务器获得响应。

找到修改后的代码,

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    new CreateUser().execute();
}

class CreateUser extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        usernamefromuser = user.getText().toString();
        passfromuser = pass.getText().toString();

        Log.e("successss", "888888888888");
        Log.e("Username", "User:" + usernamefromuser);
        Log.e("Password", "pass:" + passfromuser);

        HttpClient client = new DefaultHttpClient();

        String url = "http://54.228.149.123/api/auth";
        HttpPost httppost = new HttpPost(url);
        // httppost.setHeader("Content-type", "application/json");
        // httppost.setHeader("Accept", "application/json");

        try {
            List<NameValuePair> namevalpair = new ArrayList<NameValuePair>();
            namevalpair.add(new BasicNameValuePair("pass", passfromuser));
            namevalpair.add(new BasicNameValuePair("email",
                    usernamefromuser));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
                    namevalpair, HTTP.UTF_8);
            httppost.setEntity(entity);
            HttpResponse httpresponse = client.execute(httppost);
            responsecode = httpresponse.getStatusLine().getStatusCode();
            responseStr = EntityUtils.toString(httpresponse.getEntity());
            Log.d("Authentication", "" + responsecode);
            // Log.d("httpresponseeeee", httpresponse.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

 @Override
   protected void onPostExecute(String result) {
       if (responsecode == 200) {
        tokenStr = responseStr;
        try {
            FileOutputStream fos = openFileOutput(FILETOKEN,
                    Context.MODE_PRIVATE);
            fos.write(tokenStr.getBytes());
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (ck.isChecked()) {
            checkenords = "enable";
            try {
                FileOutputStream fos = openFileOutput(FILENAME,
                        Context.MODE_PRIVATE);
                fos.write(checkenords.getBytes());
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent inteGps = new Intent(LoginActivity.this, Gps.class);
            startActivity(inteGps);
            finish();
        } else {
            Intent inteGps = new Intent(LoginActivity.this, Gps.class);
            startActivity(inteGps);
            finish();
        }
    } else if (responsecode == 401) {

        Toast.makeText(this, "Invalid Username or Password",
                Toast.LENGTH_LONG).show();

    } else{
        Toast.makeText(this, "Please try again", Toast.LENGTH_LONG).show();
    }
   }
}