2013-02-09 64 views
2

請幫助如何使用這個。我正在進行登錄並驗證所有錯誤和可能的異常。這是我的代碼`在asynctask內烤麪包

EditText un, pw; 
ImageButton login; 
TextView error; 
ProgressDialog pd; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    un=(EditText)findViewById(R.id.txtUsername); 
    pw=(EditText)findViewById(R.id.txtPassword); 
    login=(ImageButton)findViewById(R.id.btnLogin); 
    error=(TextView)findViewById(R.id.errTxt); 


    login.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 


      new login().execute(); 


     } 



    }); 


} 
public class login extends AsyncTask{ 

    @Override 
    protected Object doInBackground(Object... params) { 
     // TODO Auto-generated method stub 

     ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
     postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
     //String valid = "1"; 
     String response = null; 


     try { 

      response = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/login.php", postParameters); 
      String res=response.toString(); 
      // res = res.trim(); 
      res= res.replaceAll("\\s+","");        
      //error.setText(res); 

      if(Integer.parseInt(res)>0){ 

      postParameters.add(new BasicNameValuePair("id", res)); 
      String result = null; 
      try 
      { 
       result = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/status.php", postParameters); 
       String res2=result.toString(); 
       res2= res2.replaceAll("\\s+","");  

       if(Integer.parseInt(res2)>0){ 

       Intent myIntent = new Intent(getApplicationContext(), dashboard.class); 
       Bundle logval = new Bundle(); 

       logval.putString("uid", res); 
       logval.putString("did", res2); 
       myIntent.putExtras(logval); 
       startActivity(myIntent); 
       }else{ 

        Toast.makeText(getApplicationContext(),"No Delivery", Toast.LENGTH_LONG).show(); 
       } 
      } 
      catch(Exception e)   
      { 

       Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show(); 
      } 
      } 
      else 
      { 

       Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 

       Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 



} 

當祝酒要執行時,程序將關閉並提示錯誤。

回答

2

您需要在主用戶界面線程中執行此操作。像這樣

MainActivity.this.runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             Toast.makeText(MainActivity.this,"call your toast from the main ui thread",300).show(); 
            } 
           }); 
+0

非常感謝。這有助於我的問題。 – 2013-02-12 00:49:51

+0

當然!如果這有幫助,你可以通過在我的帖子的左上角打勾來標記我的帖子:-) – 2013-02-12 02:26:57

+0

我想感謝你...你救了我的生命^^ – 2013-03-08 08:53:42

0

因爲您沒有發佈錯誤,所以不能確定,但​​作爲一個經驗法則您不應該從後臺線程修改UI。我猜想烤麪包可以用作UI修改嗎?

嘗試從onPostExecuteonProgressUpdate方法

1

doInBackground是一個後臺非UI線程,你不能從那裏做UI活動

另一方面,UI線程是onPreExecute,onProgressUpdate和onPostExecute。你的用戶界面活動應該只發生在這三個功能之一中。

您應該嘗試在doInBackground()中設置一個標誌,然後檢查onPostExecute中的標誌值並相應地顯示Toast消息。

+0

我嘗試這樣做,但似乎我必須儘量減少使用http客戶端時,它似乎是錯誤,當我把它放在那個函數上。 – 2013-02-09 15:05:43

+0

我很抱歉,但我真的不明白。你能用你正在嘗試的新代碼更新你的問題嗎?因爲我認爲HttpClient沒有任何問題。 – Swayam 2013-02-09 15:21:55