Android AsyncTask 似乎在 onPreExecute 之后停止

发布于 2024-12-06 22:13:54 字数 3845 浏览 1 评论 0原文

我在 Android 中使用 AsyncTasks 时遇到一些问题。我搜索了几个小时寻找解决方案,但我不明白...... 任务应通过 HTTP 检查帐户是否存在,并通过 SharedPreferences 保护访问密钥。

这是 AsyncTask 代码:(不是集成类!)

package de.test.barcode;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

public class LoginTask extends AsyncTask<Void, Void, Void> {

    private String username, password;
    private ProgressBar progressBar;
    private SharedPreferences accPrefs;

    public LoginTask(String username, String password, ProgressBar progressBar,     SharedPreferences accPrefs) {
        this.username = username.trim();
        this.password = password.trim();
        this.progressBar = progressBar;
        this.accPrefs = accPrefs;
    }

    protected Void doInBackground(Void... params) {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        InputStream is = null;

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://{URL}");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            SharedPreferences.Editor prefEditor = accPrefs.edit();  
            prefEditor.putBoolean("is_using_account", true);  
            prefEditor.putString("username", username);
            prefEditor.putString("password", password);
            prefEditor.putString("access_key", "null");
            prefEditor.commit();
        }

        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            line = reader.readLine();
            sb.append(line);
            is.close();
            String accessKey = sb.toString();
            if(!accessKey.equals("null")) {
                SharedPreferences.Editor prefEditor = accPrefs.edit();  
                prefEditor.putBoolean("is_using_account", true);  
                prefEditor.putString("username", username);
                prefEditor.putString("password", password);
                prefEditor.putString("access_key", accessKey);
                prefEditor.commit();
            }
        }catch(Exception e){
            SharedPreferences.Editor prefEditor = accPrefs.edit();  
            prefEditor.putBoolean("is_using_account", true);  
            prefEditor.putString("username", username);
            prefEditor.putString("password", password);
            prefEditor.putString("access_key", "null");
            prefEditor.commit();
        }
        return null;
    }

    protected void onPreExecute(){
        progressBar.setVisibility(View.VISIBLE);
    }

    protected void onPostExecute() {
        progressBar.setVisibility(View.GONE);
    }
}

下面是(应该)执行 AsyncTask 的行:(所有元素都定义正确)

LoginTask login = new LoginTask(username, password, progressBar, accountSettings);
login.execute();

I've some problems with AsyncTasks in Android. I searched for hours for a solution but I don't get it...
The Task should check via HTTP an account exists and safe an access key via SharedPreferences.

Here is the AsyncTask code: (not integrated class!)

package de.test.barcode;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

public class LoginTask extends AsyncTask<Void, Void, Void> {

    private String username, password;
    private ProgressBar progressBar;
    private SharedPreferences accPrefs;

    public LoginTask(String username, String password, ProgressBar progressBar,     SharedPreferences accPrefs) {
        this.username = username.trim();
        this.password = password.trim();
        this.progressBar = progressBar;
        this.accPrefs = accPrefs;
    }

    protected Void doInBackground(Void... params) {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        InputStream is = null;

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://{URL}");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            SharedPreferences.Editor prefEditor = accPrefs.edit();  
            prefEditor.putBoolean("is_using_account", true);  
            prefEditor.putString("username", username);
            prefEditor.putString("password", password);
            prefEditor.putString("access_key", "null");
            prefEditor.commit();
        }

        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            line = reader.readLine();
            sb.append(line);
            is.close();
            String accessKey = sb.toString();
            if(!accessKey.equals("null")) {
                SharedPreferences.Editor prefEditor = accPrefs.edit();  
                prefEditor.putBoolean("is_using_account", true);  
                prefEditor.putString("username", username);
                prefEditor.putString("password", password);
                prefEditor.putString("access_key", accessKey);
                prefEditor.commit();
            }
        }catch(Exception e){
            SharedPreferences.Editor prefEditor = accPrefs.edit();  
            prefEditor.putBoolean("is_using_account", true);  
            prefEditor.putString("username", username);
            prefEditor.putString("password", password);
            prefEditor.putString("access_key", "null");
            prefEditor.commit();
        }
        return null;
    }

    protected void onPreExecute(){
        progressBar.setVisibility(View.VISIBLE);
    }

    protected void onPostExecute() {
        progressBar.setVisibility(View.GONE);
    }
}

And here are the lines which (should) execute the AsyncTask: (all elements are defined right)

LoginTask login = new LoginTask(username, password, progressBar, accountSettings);
login.execute();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-12-13 22:13:54

我通常必须executeOnExecutor才能让它像这样工作,

new PlayActivity().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

请参阅

Android SDK AsyncTask doInBackground未运行(子类)

i usually have to executeOnExecutor to get it to work like this

new PlayActivity().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

see this

Android SDK AsyncTask doInBackground not running (subclass)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文