2013-02-09 1 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

기본 Userinterface 스레드에서 수행해야합니다. 이런이

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

고맙습니다. 이것은 내 문제를 해결하는 데 도움이됩니다. –

+0

그래! 이 도움이된다면 내 게시물의 왼쪽 상단에 체크 표시하여 내 게시물을 표시 할 수 있습니다 :-) –

+0

감사합니다 ... 내 인생을 살리십시오 ^^ –

0

실수를 게시하지 않았으므로, 어림짐작으로 사용자는 배경 스레드에서 UI를 수정하면 안됩니다. 나는 UI 수정으로 자격이 축배를 추측 할 것입니까?

비동기 작업의 onBackground 방법이다 onPostExecute 또는 onProgressUpdate 방법

0

당신은 백그라운드 스레드에서 토스트 수 없습니다에서 토스트 (및 대시 보드 활동에 대한 의도를) 시작하십시오. http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground(Params...).

당신은 예를 들어 그들이 UI 스레드에서 실행하고 거기에서

protected void onPostExecute (Result result) 

또는

protected void onProgressUpdate (Progress... values) 

와 토스트를 오버라이드 (override) 할 수있다.

1

doInBackground은 배경 UI가 아닌 스레드이고 은 UI 활동을 수행 할 수 없습니다..

반면에 UI 스레드는 onPreExecute, onProgressUpdate 및 onPostExecute입니다. UI 활동은이 세 가지 기능 중 하나에서만 발생해야합니다.

에 doloBackground() 안에 플래그를 설정하고 onPostExecute에있는 플래그의 값을 확인하고 이에 따라 토스트 메시지를 표시해야합니다.

+0

나는 그것을하려고하지만 내가 그 기능에 넣을 때 그것은 오류가 될 것으로 보인다 HTTP 클라이언트의 사용을 최소화해야 할 것으로 보인다. –

+0

미안하지만 이해가 안됩니다. 지금 시도하고있는 새로운 코드로 질문을 업데이트 할 수 있습니까? 왜냐하면 나는 HttpClient에 어떤 문제가 있다고 생각하지 않기 때문이다. – Swayam