1)我已经在这里阅读了所有关于此的问题,所以请告诉我更多,而不仅仅是来自SO的网址...
2)我想从asynctask中只返回一个变量,但它可能是消息错误或用户详细信息。所以我决定返回ArrayList,索引0(用于错误消息)和1(用于成功)。
的AsyncTask
public class LoginGet extends AsyncTask<String, ProgressBar, ArrayList<String>> {
...
public ArrayList<String> result = new ArrayList<String>();
public LoginGet(Context mContext, String loginText, String passwordText) {
//Get values form LoginActivity
}
protected void onPreExecute(){
super.onPreExecute();
// progressDialog
}
@Override
protected ArrayList doInBackground(String... arg0){
try {
// Making HTTP Request
try {
// Creating HTTP client
...
...
JSONObject jObject = new JSONObject(json);
success = jObject.getString("success");
result.add(0, "");
result.add(1, "");
if (success != null) {
if (success == "1") {
result.add(1, jObject.getString("hash"));
return result;
} else if (success == "0") {
result.add(0, jObject.getString("error"));
return result;
}
} else {
result.add(0, "Error while logging.");
return result;
}
// writing response to log
Log.d("Http Response:", response.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
}
return result;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
((LoginActivity)context).getResult(result);
progressDialog.dismiss();
}
}
LoginActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
...
if (loginText.compareTo("") == 0 || passwordText.compareTo("") == 0) {
...
}else{
LoginGet loginPost = new LoginGet(LoginActivity.this,loginText,passwordText);
loginPost.execute();
}
}
});
}
// Get result from asynctask LoginGet
public void getResult(ArrayList<String> result){
if (result.get(0) != "") {
loginButton.setEnabled(true);
Toast.makeText(LoginActivity.this, result.get(0), Toast.LENGTH_SHORT).show();
} else if (result.get(1) != "") {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Login", result.get(1));
editor.commit();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
错误:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at cz.jacon.smsapp.LoginActivity.getResult(LoginActivity.java:57)
at cz.jacon.smsapp.background.LoginGet.onPostExecute(LoginGet.java:121)
at cz.jacon.smsapp.background.LoginGet.onPostExecute(LoginGet.java:30)
3)在我的AsyncTask中,我有result.add(0, "");
和result.add(1, "");
,为什么ArrayList的大小为0?
答案 0 :(得分:0)
在我看来,result.add(0, "");
甚至无法到达,因为try块可能会失败。
遗憾你有一个空的catch块,只是等着跳过它们错误:
catch (Exception e) {
}
您的解决方案是添加这些
result.add(0, "");
result.add(1, "");
到doInBackground
的开头,甚至是构造函数,你应该没问题。