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