2014-02-25 63 views
0

我在SQLite數據庫中存儲了一些值,並嘗試通過按鈕單擊將它發送到MySQL。以下是相關的代碼Eclipse說無法執行操作,因爲連接池關閉

MainActivity.java

private class SendCollections extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 

      JSONArray json = new JSONArray(); 
      for(int i = 0; i < dbHandler.getCollectionCount(); i++){ 
       Bundle data = dbHandler.getCollectionDetails(); 

       JSONObject jObject = new JSONObject(); 
       try { 
        jObject.put("cust_id", data.getString("cust_id")); 
        jObject.put("col_id", colId); 
        jObject.put("method", data.getString("method")); 
        jObject.put("amount", data.getInt("amount")); 
        jObject.put("check_no", data.getInt("cheq_no")); 

        json.put(jObject); 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }      
      } 
      Log.d("Created JSON Array", json.toString()); 
      //sendJson("http://192.168.100.172/android/json_payments.php", json); 
      UserFunctions userFunctions = new UserFunctions(); 
      userFunctions.syncCollections(json.toString()); 

      // TODO Auto-generated method stub 
      return null; 
     } 



} 

我執行按鈕點擊的AsyncTask。

UserFunctions.java

public JSONObject syncCollections(String jsonArray){ 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("collections", jsonArray)); 
     JSONObject json = jsonParser.getJSONFromUrl(sync_collections, params); 

     Log.d("Sending JSON Array", jsonArray); 

     return json; 
    } 

JSONParser.java

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
      Log.d("JSON", json); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 

DatabaseHandler.java

public Bundle getCollectionDetails(){ 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Bundle cursorData = new Bundle(); 
     String selectQuery = "SELECT * FROM " + TABLE_COLLECTION; 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     if(cursor.moveToFirst()){ 
      cursorData.putString("cust_id", cursor.getString(cursor.getColumnIndex(KEY_CUSTOMER_ID))); 
      if(cursor.getInt(cursor.getColumnIndex(KEY_CHEQ_NO)) == 0){ 
       cursorData.putString("method", "Cash"); 
      }else{ 
       cursorData.putString("method", "Cheque"); 
      } 
      cursorData.putInt("amount", cursor.getInt(cursor.getColumnIndex(KEY_AMOUNT))); 
      cursorData.putInt("cheq_no", cursor.getInt(cursor.getColumnIndex(KEY_CHEQ_NO))); 
     } 

     return cursorData; 
    } 

public int getCollectionCount(){ 
     String countQuery = "SELECT * FROM " + TABLE_COLLECTION; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     int rowCount = cursor.getCount(); 
     db.close(); 
     cursor.close(); 

     return rowCount; 
    } 

當「集合」表中只有一個條目時,它們似乎沒有錯誤(如在logcat中顯示「發送JSON數組:」)。但是,當有更多的條目,在logcat中提供了以下錯誤

logcat的

02-25 10:12:05.660: E/AndroidRuntime(11394): FATAL EXCEPTION: AsyncTask #4 
02-25 10:12:05.660: E/AndroidRuntime(11394): Process: collector.lbfinance, PID: 11394 
02-25 10:12:05.660: E/AndroidRuntime(11394): java.lang.RuntimeException: An error occured while executing doInBackground() 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.os.AsyncTask$3.done(AsyncTask.java:300) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.lang.Thread.run(Thread.java:841) 
02-25 10:12:05.660: E/AndroidRuntime(11394): Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed. 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at collector.lbfinance.library.DatabaseHandler.getCollectionCount(DatabaseHandler.java:355) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at collector.lbfinance.AgentHome$SendCollections.doInBackground(AgentHome.java:273) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at collector.lbfinance.AgentHome$SendCollections.doInBackground(AgentHome.java:1) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
02-25 10:12:05.660: E/AndroidRuntime(11394): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
02-25 10:12:05.660: E/AndroidRuntime(11394): ... 4 more 
+0

在DatabaseHandler.java只使用cursor.moveToFirst。它只會到你的表的第一行。爲什麼不使用while(cursor.moveToNext)遍歷所有行? – user3213851

+0

哦,是的。一個簡單的do {<上面的代碼塊>} while(cursor.moveToNext())會做對嗎? –

+0

我不明白你的問題。你能粗略估計你想要達到的目標嗎?就像你的數據庫中有多少行,如果你點擊一個按鈕應該選擇一個特定的行或所有的行被選中? – user3213851

回答

2

編輯方法GetCollectionDetails如下。

public Bundle getCollectionDetails(){ 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Bundle cursorData = new Bundle(); 
     String selectQuery = "SELECT * FROM " + TABLE_COLLECTION; 

     Cursor cursor = db.rawQuery(selectQuery, null); 
     int no_of_rows=cursor.getCount();   //count the no of rows in the database 
     String[] cust_id=new String[row_count]; 
     String[] method=new String[row_count] ; 
     int[] amount=new int[row_count]  
     int[] cheq_no=new int[row_count] 
     int i=0; 

     if(cursor.moveToFirst()){ 
      cust_id[i]= cursor.getString(cursor.getColumnIndex(KEY_CUSTOMER_ID)); 
      if(cursor.getInt(cursor.getColumnIndex(KEY_CHEQ_NO)) == 0){ 
       method[i]="Cash"; 
      }else{ 
       mehod[i]="Cheque"; 
      } 
      amount[i]= cursor.getInt(cursor.getColumnIndex(KEY_AMOUNT)); 
      cheq_no[i]= cursor.getInt(cursor.getColumnIndex(KEY_CHEQ_NO)); 
      i++; 
     } 
     cursorData.putStringArray("cust_id",cust_id); 
     cursorData.putStringArray("method",method); 
     cursorData.putIntArray("amount",amount); 
     cursorData.putIntArray("cheq_no",cheq_no); 
     return cursorData; 
    } 

然後在MainActivity.java你應該寫下面的代碼。

int no_of_rows=dbHandler.getCollectionCount(); 
String[] cust_id=new String[row_count]; String[] method=new String[row_count] ; int[] amount=new int[row_count] 
int[] cheq_no=new int[row_count] int i=0; Bundle data = dbHandler.getCollectionDetails(); cust_id=data.getStringArray("cust_id"); //simililarily retrive for others 

for(int i = 0; i < dbHandler.getCollectionCount(); i++){ Bundle data = dbHandler.getCollectionDetails(); 

    JSONObject jObject = new JSONObject(); 
    try { 
     jObject.put("cust_id", cust_id[i]); 
     json.put(jObject); 
    } catch (JSONException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }      
} 

我希望這應該可以正常工作了你..

+0

Eclipse在.putString中顯示語法錯誤。 cust_id [i] = cursor.getString(cursor.getColumnIndex(KEY_CUSTOMER_ID))應該是正確的方法嗎?而且,不應該在一段時間內做什麼? –

+0

看看它現在工作。順便說一句,我沒有測試過我的日食代碼。我只是想給你提供邏輯。可能有一些編譯錯誤。如果您遇到更多困難,請告訴我。 – user3213851

+0

謝謝。我可以看到爲什麼我的方法是愚蠢和錯誤的,這應該工作。但我仍然得到以前的錯誤。即使使用最初的代碼,至少它應該輸出重複的權利?但是我仍然(甚至在改變它之後)得到與OP –

相關問題