【问题标题】:How to use Async task with existing code如何将异步任务与现有代码一起使用
【发布时间】:2014-01-05 04:47:35
【问题描述】:

我在尝试将AsyncTask 应用于我现有的代码时遇到了困难。

所以我先说我的代码运行良好,直到我升级了我的目标 SDK,然后我收到了以下错误消息:

android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)

经过一些研究,看起来这是因为我试图在主线程上运行网络操作,这是一个很大的禁忌。对此的补救措施是使用异步任务来运行网络操作。

好的,到目前为止对我来说很有意义,现在我要做的就是以某种方式在我的代码中实现异步任务,这就是我的问题。

基本上,我有一个登录屏幕,可以在成功登录后进入主页(有点像 Facebook)。当您单击登录按钮时,它会向我服务器上的 PHP 文件发送 HTTP 请求,该文件验证登录名/密码并返回响应。根据该响应,它会让您登录(或为您提供“无效登录”响应)。

所以我很确定这是罪魁祸首。现在,我的问题是,如何以异步方式运行此任务?我不是在寻找任何人来编写我的代码或任何东西,我只是在寻找一些关于如何开始的指导。我已经在这工作了几天了,我很兴奋:(。

这是我的登录类的代码,你可以在底部看到我的异步:

public class AndroidLogin extends Activity implements OnClickListener {


    Button ok,back,exit;
    TextView result;




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);



        // Login button clicked
        ok = (Button)findViewById(R.id.btn_login);
        ok.setOnClickListener(this);


        result = (TextView)findViewById(R.id.tbl_result);



    }









    public void postLoginData() {

        // Add user name and password
        EditText uname = (EditText)findViewById(R.id.txt_username);
          String username = uname.getText().toString();

        EditText pword = (EditText)findViewById(R.id.txt_password);
        String password = pword.getText().toString();

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();


        // login.php returns true if username and password match in db 
        HttpPost httppost = new HttpPost("http://www.alkouri.com/android/login.php?username=" + username + "&password=" + password  );

        try {






            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent()).toString();
            Log.w("SENCIDE", str);

            if(str.toString().equalsIgnoreCase("true"))
            {
                Log.w("SENCIDE", "TRUE");
                result.setText("Login Successful! Please Wait...");   
            }else
            {
                Log.w("SENCIDE", "FALSE");
                result.setText(str);                
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { 
                total.append(line); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }

    //when register button is clicked
    public void RegisterButton(View view) {
        Intent myIntent = new Intent(AndroidLogin.this, Registration.class);
        AndroidLogin.this.startActivity(myIntent);

    }







    protected void onPostExecute(Void v){
        // turns the text in the textview "Tbl_result" into a text string called "tblresult"
        TextView tblresult = (TextView) findViewById(R.id.tbl_result);
        // If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
           if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
                 Intent intent = new Intent();
                //take text in the username/password text boxes and put them into an extra and push to next activity 
                 EditText uname2 = (EditText)findViewById(R.id.txt_username);
                 String username2 = uname2.getText().toString();
                 EditText pword2 = (EditText)findViewById(R.id.txt_password);
                 String password2 = pword2.getText().toString();
                 intent.putExtra("username2", username2 + "&pword=" + password2);
                 startActivity(intent);
              }    
   }













    public void onClick(View view) {

        class PostLogingDataTask extends AsyncTask<Void,Void,Void>  
        { 
            protected Void doInBackground (Void... t)
            {
                postLoginData();
                return null;





            }



           }

                new PostLogingDataTask ().execute();
    }


}

这是我得到的错误:

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

【问题讨论】:

  • 你的代码中没有异步任务。
  • 我刚刚修改了我的代码,我把异步任务放在里面@Raghunandan
  • 无法从 nnackground 线程更新/访问 ui。这就是你得到异常的原因

标签: java android asynchronous android-asynctask


【解决方案1】:
       public void onClick(View view) {

    class PostLogingDataTask extends AsyncTask<Void,Void,Void>  
    { 
        protected Void doInBackground (Void... t)
        {
            postLoginData();
        }


          protected void onPostExecute(Void v){
             // turns the text in the textview "Tbl_result" into a text string called "tblresult"
             TextView tblresult = (TextView) findViewById(R.id.tbl_result);
             // If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
                if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
                      Intent intent = new Intent(this, Homepage.class);
                     //take text in the username/password text boxes and put them into an extra and push to next activity 
                      EditText uname2 = (EditText)findViewById(R.id.txt_username);
                      String username2 = uname2.getText().toString();
                      EditText pword2 = (EditText)findViewById(R.id.txt_password);
                      String password2 = pword2.getText().toString();
                      intent.putExtra("username2", username2 + "&pword=" + password2);
                      startActivity(intent);
                   }    
        }
       }

            new PostLogingDataTask ().execute();
}

【讨论】:

  • 除非您将 textview 的 setText 放回主线程上,否则这将不起作用...使用 onPostExecute developer.android.com/reference/android/os/AsyncTask.html
  • 只是另一条评论 - onPostExecute 需要在 asynctask 类中,并且 doInBackground 的返回需要返回响应,以便 post execute 方法可以使用它。
  • 我试过这个,Eclipse 要求我在我所做的postLoginData(); 之后添加一个return null;。现在我收到以下错误:FATAL EXCEPTION: AsyncTask #1 01-04 21:10:25.017: E/AndroidRuntime(5599): java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) 01-04 21:10:25.017: E/AndroidRuntime(5599): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
  • 不要修改 doInBackgroundThread 线程中的任何 UI 元素,而是返回关于成功登录的结果表单 doInBackgroundThread。并在 onPostExecute 中进行所有 UI 更新。
【解决方案2】:

使用线程进行网络操作,使用处理程序处理 UI 线程

new Thread(new runnable(){
    @Override
    public void run() {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        Log.w("SENCIDE", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httppost);

        String str = inputStreamToString(response.getEntity().getContent()).toString();
        Log.w("SENCIDE", str);
        Message msg = new Message();
        Bundle bundle = new Bundle();
        bundle.putString("str",str);
        msg.setData(bundle);
        handler.sendMessage(msg);   
    }
}).start();

Handler handler = new Handler() {  
    public void handleMessage(Message msg) {  
    super.handleMessage(msg); 
        str = msg.getData().getString("str"); 
        if(str.toString().equalsIgnoreCase("true"))
        {
            Log.w("SENCIDE", "TRUE");
            result.setText("Login Successful! Please Wait...");   
        }else
        {
            Log.w("SENCIDE", "FALSE");
            result.setText(str);                
        }
    }   
};

【讨论】:

  • 使用这个和使用异步任务有什么区别?
  • 没有区别。但是使用 Thread 你只做网络操作,如果你需要改变 UI 你需要使用处理程序
猜你喜欢
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多