我在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();
答案 0 :(得分:0)
我通常必须执行OpeExecutor才能让它像这样工作
new PlayActivity().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
看到这个