Android AsyncTask 似乎在 onPreExecute 之后停止
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常必须executeOnExecutor才能让它像这样工作,
请参阅
Android SDK AsyncTask doInBackground未运行(子类)
i usually have to executeOnExecutor to get it to work like this
see this
Android SDK AsyncTask doInBackground not running (subclass)