我正在尝试运行一个服务,该服务将连接到一个网站并从网站获取特定数据到android设备sqlite数据库。它工作正常。但就像我一样,它有时会出现这个错误。但是当服务再次运行时,它会像往常一样运行良好。我根本找不到什么是错误。此外,这是我第一次写这么多复杂的服务,请告诉我是否有任何有效的方法。
public class Serviceclass extends Service {
protected static final String MyPREFERENCES = null;
private boolean isRunning;
private Context context;
private Thread backgroundThread;
private float n;
private ArrayList<String> xx = new ArrayList<String>();
private Document doc;
private String checkdate;
DatabaseHandler db = new DatabaseHandler(this);
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
public void run() {
SharedPreferences myPrefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String date = myPrefs.getString("Date", null);
if (date != null) {
System.out.println("its not empty");
// check if the dates are a match
match(date);
} else {
System.out.println("its empty");
update();
}
stopSelf();
}
};
@Override
public void onDestroy() {
this.isRunning = false;
}
protected void match(String date) {
check();
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
checkdate = sharedpreferences.getString("Date", null);
System.out.println(checkdate);
if (date.contentEquals(checkdate)) {
System.out.println("Dates are the same");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
} else {
System.out.println("Dates are not the same");
update();
}
}
private void check() {
// TODO Auto-generated method stub
try {
doc = Jsoup.connect("http://www.xoxoxoxoxox.com/index.php").get();
Log.i("tag", "post");
Elements myin = doc.getElementsByClass("rates_text_home");
for (Element element : myin) {
String a = element.text();
xx.add(a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void update() {
check();
Log.i("tag", xx.get(0));
System.out.println("Loading Data");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("Date", xx.get(0));
editor.commit();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
}
以下是错误的logcat输出。
FATAL EXCEPTION: Thread-13
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at com.example.marketapp.Serviceclass.match(Serviceclass.java:82)
at com.example.marketapp.Serviceclass$1.run(Serviceclass.java:53)
at java.lang.Thread.run(Thread.java:1019)
答案 0 :(得分:1)
您的列表xx有0个元素,您尝试访问第一个 - 这是您的问题
也许你在check:
中遇到了这个IOException➜ ~ curl http://www.xoxoxoxoxox.com/index.php
curl: (6) Could not resolve host: www.xoxoxoxoxox.com
答案 1 :(得分:1)
您永远不会向ArrayList xx
添加元素。因此匹配函数中的列表为空。
答案 2 :(得分:1)
protected void update() {
check();
if(xx.size() > 0){
Log.i("tag", xx.get(0));
System.out.println("Loading Data");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("Date", xx.get(0));
editor.commit();
}
}
使用上面的代码并检查