0

我正在向前迈出一些脚步,但是我遇到了一些问题,当我调用服务时,它应该连接到一个 json 资源,获取结果并将它们写入数据库,嗯......接缝什么都没有发生:

电话:

protected void onListItemClick(ListView l, View v, int position, long id) {
    //Toast.makeText(this, String.valueOf(strdomanda), Toast.LENGTH_LONG).show(); 
    datasource.setRispostaSelezionata(Long.toString(id));   
    datasource.setDomandaFatta(strdomanda);
    datasource.close();
    Intent intent = new Intent(this, updateDemocracyService.class);
    startService(intent);

    finish();



    }

服务 :

public class updateDemocracyService extends IntentService{
    public updateDemocracyService() {
        super("updateDemocracyService");
        // TODO Auto-generated constructor stub
    }
    private pollDataSource datasource;
     int mStartMode;       // indicates how to behave if the service is killed
        IBinder mBinder;      // interface for clients that bind
        boolean mAllowRebind; // indicates whether onRebind should be used
        // url to make request
        private static String url = "http://lnx.ggservice.com/democracy/domande.php";

        // JSON Node names
        private static final String TAG_DOMANDE = "domande";
        private static final String TAG_ID = "id";
        private static final String TAG_TESTO = "testo";


        // contacts JSONArray
        JSONArray contacts = null;


        @Override
        protected void onHandleIntent(Intent intent) {
            datasource = new pollDataSource(this);
            datasource.open();
            Thread t = new Thread(){
                public void run(){

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(url);

            try {

                // Getting Array of Contacts
                contacts = json.getJSONArray(TAG_DOMANDE);

                // looping through All Contacts
                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_TESTO);
                    datasource.createCategoria(name);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

                }
            };
            t.start();


            Toast.makeText(this,"faccio cose", Toast.LENGTH_LONG).show();   


        }


}

服务可能会启动,我不知道如何检查,但实际上它什么也没做,也许它连接到服务器,但很可能我弄错了什么......你看到有什么问题吗?为什么服务什么都不做?在此之前我正在执行中的大部分代码onStartCommand,但我看到我必须使用onHandleIntent

更新:

现在它给出了这些错误:

01-03 09:28:39.582: D/dalvikvm(17584): GC_CONCURRENT freed 157K, 3% free 9328K/9607K, paused 13ms+15ms
01-03 09:28:40.162: W/dalvikvm(17584): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
01-03 09:28:40.242: E/AndroidRuntime(17584): FATAL EXCEPTION: Thread-124
01-03 09:28:40.242: E/AndroidRuntime(17584): java.lang.NullPointerException
01-03 09:28:40.242: E/AndroidRuntime(17584):    at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
01-03 09:28:40.242: E/AndroidRuntime(17584):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:115)
01-03 09:28:40.242: E/AndroidRuntime(17584):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
01-03 09:28:40.242: E/AndroidRuntime(17584):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
01-03 09:28:40.242: E/AndroidRuntime(17584):    at com.ggservice.democracy.pollDataSource.createCategoria(pollDataSource.java:41)
01-03 09:28:40.242: E/AndroidRuntime(17584):    at com.ggservice.democracy.updateDemocracyService$1.run(updateDemocracyService.java:63)
01-03 09:28:41.851: W/MessageQueue(17584): Handler (android.os.Handler) {412f6590} sending message to a Handler on a dead thread
01-03 09:28:41.851: W/MessageQueue(17584): java.lang.RuntimeException: Handler (android.os.Handler) {412f6590} sending message to a Handler on a dead thread
01-03 09:28:41.851: W/MessageQueue(17584):  at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.os.Handler.sendMessageAtTime(Handler.java:473)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.os.Handler.sendMessageDelayed(Handler.java:446)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.os.Handler.post(Handler.java:263)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.widget.Toast$TN.hide(Toast.java:358)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
01-03 09:28:41.851: W/MessageQueue(17584):  at android.os.Binder.execTransact(Binder.java:338)
01-03 09:28:41.851: W/MessageQueue(17584):  at dalvik.system.NativeStart.run(Native Method)

数据源:

public class pollDataSource {
    // Database fields
      private SQLiteDatabase database;
      private MySQLiteHelper dbHelper;
      private String[] allCategorieColumns = { MySQLiteHelper.COLUMN_ID,
          MySQLiteHelper.COLUMN_PREF, MySQLiteHelper.COLUMN_NOME };   
      private String[] allSondaggiColumns = { MySQLiteHelper.COLUMN_ID,
              MySQLiteHelper.COLUMN_CATID,MySQLiteHelper.COLUMN_WEBID,MySQLiteHelper.COLUMN_FATTA, MySQLiteHelper.COLUMN_DOMANDA };   
      private String[] allRisposteColumns = { MySQLiteHelper.COLUMN_ID, 
              MySQLiteHelper.COLUMN_SONDID,MySQLiteHelper.COLUMN_WEBID, MySQLiteHelper.COLUMN_RISPOSTA, 
              MySQLiteHelper.COLUMN_SELEZIONATA };


      public pollDataSource(Context context) {
            dbHelper = new MySQLiteHelper(context);
          }

      public void open() throws SQLException {
            database = dbHelper.getWritableDatabase();
          }

      public void close() {
            dbHelper.close();
          }

      public categorie createCategoria(String categoria) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_NOME, categoria);
            values.put(MySQLiteHelper.COLUMN_PREF, 0);
            long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
                values);
            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            categorie newCategoria = cursorToCategorie(cursor);
            cursor.close();
            return newCategoria;
          } 

      public void deleteCategoria(categorie categoria) {
            long id = categoria.getId();
            System.out.println("Categoria cancellata, id: " + id);
            database.delete(MySQLiteHelper.TABLE_CATEGORIE, MySQLiteHelper.COLUMN_ID
                + " = " + id, null);
          }

      public sondaggi createSondaggio(String domanda, int catid, int webid) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_DOMANDA, domanda);
            values.put(MySQLiteHelper.COLUMN_CATID, catid);
            values.put(MySQLiteHelper.COLUMN_WEBID, webid);
            values.put(MySQLiteHelper.COLUMN_FATTA, "0");
            long insertId = database.insert(MySQLiteHelper.TABLE_SONDAGGI, null,
                values);
            Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                allSondaggiColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            sondaggi newSondaggio = cursorToSondaggi(cursor);
            cursor.close();
            return newSondaggio;
          }


      public void deleteSondaggio(sondaggi sondaggio) {
            long id = sondaggio.getId();
            System.out.println("Sondaggio cancellato, id: " + id);
            database.delete(MySQLiteHelper.TABLE_SONDAGGI, MySQLiteHelper.COLUMN_ID
                + " = " + id, null);
          } 

      public Cursor getAllCategorie() {
            List<categorie> categorie = new ArrayList<categorie>();

            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, null, null, null, null, null);

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              categorie categoria = cursorToCategorie(cursor);
              categorie.add(categoria);
              cursor.moveToNext();
            }
            // Make sure to close the cursor
           // cursor.close();
            return cursor;
          }

      public Cursor getDomanda(String id) {
            List<sondaggi> domande = new ArrayList<sondaggi>();


            Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                    allSondaggiColumns,
                    MySQLiteHelper.COLUMN_ID + "=?",
                    new String[] { id }, null, null, null);

            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
              sondaggi sondaggio = cursorToSondaggi(cursor);
              domande.add(sondaggio);
              cursor.moveToNext();
            }

            return cursor;
          }


      private categorie cursorToCategorie(Cursor cursor) {
          categorie categorie = new categorie();
          categorie.setId(cursor.getLong(0));
          categorie.setPreferita(cursor.getLong(1));
          categorie.setNome(cursor.getString(2));
            return categorie;
          }


      private sondaggi cursorToSondaggi(Cursor cursor) {
          sondaggi sondaggi = new sondaggi();
          sondaggi.setId(cursor.getLong(0));
          sondaggi.setDomanda(cursor.getString(1));
          sondaggi.setCatid(cursor.getLong(2));
          sondaggi.setwebid(cursor.getLong(3));
          return sondaggi;
          }

    public Cursor getAllDomandeCategoria(String catid) {
        List<sondaggi> domande = new ArrayList<sondaggi>();


        Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
                allSondaggiColumns,
                MySQLiteHelper.COLUMN_CATID + "=? AND " + MySQLiteHelper.COLUMN_FATTA + "='0'",
                new String[] { catid }, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
          sondaggi sondaggio = cursorToSondaggi(cursor);
          domande.add(sondaggio);
          cursor.moveToNext();
        }

        return cursor;
    }

    public Cursor getTestoRisposte(String strdomanda) {

        List<testo_risposte> domande = new ArrayList<testo_risposte>();    
        Cursor cursor = database.query(MySQLiteHelper.TABLE_TESTORISPOSTE,
                allRisposteColumns,
                MySQLiteHelper.COLUMN_SONDID + "=?",
                new String[] { strdomanda }, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
         testo_risposte sondaggio = cursorToTestoRisposte(cursor);
          domande.add(sondaggio);
          cursor.moveToNext();
        }

        return cursor;
    }

    private testo_risposte cursorToTestoRisposte(Cursor cursor) {
        testo_risposte testo_risposte = new testo_risposte();
        testo_risposte.setId(cursor.getLong(0));
        testo_risposte.setCatid(cursor.getLong(1));
        testo_risposte.setWebid(cursor.getLong(4));
        testo_risposte.Setselezionata(cursor.getLong(2));
        testo_risposte.setDomanda(cursor.getString(3));
     return testo_risposte;
    }

    public void setRispostaSelezionata(String id) {     
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_SELEZIONATA, "1");
        database.update(MySQLiteHelper.TABLE_TESTORISPOSTE,values,MySQLiteHelper.COLUMN_ID + "=?", new String[] { id });    
    }

    public void setDomandaFatta(String strdomanda) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_FATTA, "1");
        database.update(MySQLiteHelper.TABLE_SONDAGGI,values,MySQLiteHelper.COLUMN_ID + "=?", new String[] { strdomanda });

    }


}
4

0 回答 0