Java Eclipse表示无法执行操作,因为连接池已关闭
我正在SQLite数据库中存储一些值,并试图在单击按钮时将其发送到MySQL。以下是相关代码 MainActivity.javaJava Eclipse表示无法执行操作,因为连接池已关闭,java,android,json,sqlite,Java,Android,Json,Sqlite,我正在SQLite数据库中存储一些值,并试图在单击按钮时将其发送到MySQL。以下是相关代码 MainActivity.java private class SendCollections extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { JSONArray json = new JSO
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;
}
}
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;
}
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;
}
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;
}
当“collection”表中只有一个条目时,它们似乎没有错误(如logcat中显示的“Sending JSON Array:”)。但是当有更多的条目时,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
编辑方法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();
}
}
int no_of_rows=dbHandler.getCollectionCount();
字符串[]客户id=新字符串[行计数];字符串[]方法=新字符串[行计数];整数[]金额=新整数[行计数]
int[]cheq_no=新int[行计数]int i=0;Bundle data=dbHandler.getCollectionDetails();cust_id=data.getStringArray(“cust_id”)//同样地为别人而努力
对于(int i=0;i
我希望这可以很好地为您服务。在DatabaseHandler.java中,您只使用cursor.moveToFirst。只需要到您桌子的第一排。为什么不使用while(cursor.moveToNext)遍历所有行?哦,是的。一个简单的do{}while(cursor.moveToNext())就可以了吗?我不明白你的问题。你能粗略估计一下你想要达到的目标吗。例如,数据库中有多少行,如果单击按钮,是选择某一行还是选择所有行?我将存储接收付款的详细信息。它可以是1或更多。用户通过单击“同步”按钮连接到网络时与数据库同步。我需要将详细信息发送到php文件。我要表格中所有列的所有行我已经给你提供了答案。查看这是否有效,如果有效,请接受答案。Eclipse在.putString中显示语法错误。cust\u id[i]=cursor.getString(cursor.getColumnIndex(KEY\u CUSTOMER\u id))应该是正确的方法,对吗?而且,这些不应该在里面做一段时间吗?看看它现在是否工作。顺便说一下,我还没有在eclipse上测试代码。我只是想给你一个逻辑。可能有一些编译错误。如果你还面临任何困难,请告诉我。谢谢。我明白为什么我的方法是愚蠢和错误的,这应该是可行的。但我仍然得到了以前的错误。即使是初始代码,它至少应该输出重复的代码,对吗?但是我(仍然,即使在将其更改为此)得到了与OPIt中所示相同的错误集,问题似乎是我试图从后台线程访问数据库。现在很好用。谢谢你的帮助,伙计,你太棒了