2014-01-31 55 views
0

我有一個登錄應用程序。當用戶填寫表單並單擊登錄按鈕時,它將從服務器獲取一個字符串並檢查,如果正確,則轉到下一個活動。一切正常,但第一次它不能存儲字符串。我認爲這是爲了後臺任務。後臺任務的值是字符串。所以if條件取決於該字符串值。在去下一個任務之前,我怎樣才能得到字符串。Android登錄身份驗證不起作用

這裏是我的活動:

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。請幫幫我。

+0

「它不能存儲字符串」。但是哪裏?你想在哪裏存儲它。 – user1728071

+0

在onclick方法中,我使用了一個條件if(responsecode == 200){。所以我需要字符串(responsecode)。在這種情況之前。這就是爲什麼我使用「new CreateUser()。execute();」,這應該保存「responsecode」的值。 –

回答

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

謝謝兄弟。無論如何在asynctask中使用烤麪包? –

+0

我明白了。謝謝。 –

+0

是的,你可以使用onProgressUpdate()方法顯示Toast(可以更新UI),它用於顯示後臺進程的狀態更新。只要您使用onProgressUpdate()方法在doInBackground()中發佈進度,就會調用此方法。即不等待完成doInBackground。 – Jayabal