我在安卓中使用AsyncTasks时遇到了一些问题。我已经寻找了数小时的解决方案,但是我还是无法理解……
该任务应该通过HTTP检查一个账户是否存在,并将访问密钥保存到SharedPreferences中。
以下是AsyncTask代码(未集成类!):
以下是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();
accessKey
为空。但我确定sb
不为 null :/ - TheVaan