我不知道该怎么称呼这个错误。我的应用程序使用自编写的共享首选项类。运行我的应用程序几次后,我的远程服务器无法检索到任何内容。我将发布我的共享首选项和一个我的异步任务。当应用程序没有检索任何内容时,logcat中没有错误。
共享偏好设置课程:
public class SharedPreferences{
static final String PREF_USER_NAME = "username";
static final String PREF_USER_ID = "userid";
static android.content.SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void setUserName(Context ctx, String userName)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_NAME, userName);
editor.commit();
}
public static void setUserId(Context ctx, String userId)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_ID, userId);
editor.commit();
}
public static String getUserName(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}
public static String getUserId(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_ID, "");
}
public static void clearUserName(Context ctx)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.clear(); //clear all stored data
editor.commit();
}
}
项目检索异步任务:
public class GetEventsAsyncTask extends AsyncTask<String, Integer, Boolean>{
ProgressDialog progressDialog;
MainActivity activityMain;
List<Event> eventList = new ArrayList<Event>();
MyDbAdapter db;
public GetEventsAsyncTask(MainActivity parent)
{
activityMain = parent;
}
@Override
protected void onPreExecute() {
// progressDialog = null;
//
// if (progressDialog == null)
// {
// progressDialog = new ProgressDialog(activityMain);
// progressDialog.setMessage("download events, please wait...");
// progressDialog.show();
// progressDialog.setCanceledOnTouchOutside(false);
// progressDialog.setCancelable(false);
// }
}
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean error = false;
try {
error = postData(params[0]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return error;
}
protected void onPostExecute(Boolean error){
/* if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
} */
if(error==true)
{
Log.i("GetEvents", "Error at get events");
activityMain.errorOccured();
}
else
{
Log.i("onPostExecute:eventlist.count",String.valueOf(eventList.size()));
MainActivity.myDB.removeAllEvents();
for(int i=0;i<eventList.size();i++)
{
MainActivity.myDB.insertEventEntry(eventList.get(i));
}
activityMain.downloadEventsSuccess(eventList);
// progressDialog.dismiss();
}
}
protected void onProgressUpdate(Integer... progress){
}
public Boolean postData(String user_id) throws JSONException {
Boolean error = false;
HttpClient httpclient = new DefaultHttpClient();
// specify the URL you want to post to
try {
Log.i("GetEvents", user_id);
HttpGet httpget = new HttpGet(Constants.HOST_NAME+"/"+Constants.SERVICE_NAME+"/api/Event?userId=" + user_id);
BufferedReader reader;
StringBuffer sb;
String line = "";
String NL="";
String json;
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==200)
{
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
sb = new StringBuffer("");
line = "";
NL = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
sb.append(line + NL);
}
reader.close();
json = sb.toString();
Log.i("event json",json);
try
{
JSONArray jsonArray = new JSONArray(json);
for (int i = 0, length = jsonArray.length(); i < length; i++)
{
JSONObject attribute = jsonArray.getJSONObject(i);
Log.i("EventsAsync", attribute.toString(i));
Event eventObj = new Event();
eventObj.setEvent_id(attribute.getString("event_id"));
eventObj.setEvent_title(attribute.getString("event_title"));
eventObj.setEvent_desc(attribute.getString("event_desc"));
eventObj.setStart_date(attribute.getString("start_date"));
eventObj.setEnd_date(attribute.getString("end_date"));
eventObj.setStart_time(attribute.getString("start_time"));
eventObj.setEnd_time(attribute.getString("end_time"));
eventObj.setLocation(attribute.getString("location"));
eventObj.setPicture_path(attribute.getString("picture_path"));
eventObj.setSmall_picture_path(attribute.getString("small_picture_path"));
eventList.add(eventObj);
// DatabaseUser.openWritable();
// DatabaseUser.insertEvent(eventObj);
eventObj = null;
}
}
catch (JSONException e)
{
e.printStackTrace();
error = true;
}
}
else
{
error = true;
}
}
catch (ClientProtocolException e)
{
// process execption
error = true;
}
catch (IOException e)
{
// process execption
error = true;
}
return error;
}
}
大多数情况下,应用程序会在从onStart到onDestroy之后停止检索3次。