Android AsyncTask seems to stop after onPreExecute
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();
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)
精彩评论