Reputation: 585
i am executing sql query using JDBC driver but it gives me NetworkOnMainThreadException.i have used a Asyntask for the network operation and mention Internet permission in the menifest file. below is my code for executing query
public class getLedger extends AsyncTask<String, Void, Void> {
private ProgressDialog progressDlg;
private ResultSet rs;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDlg = new ProgressDialog(LedgerActivity.this);
progressDlg.setMessage("Please wait...");
progressDlg.setIndeterminate(false);
progressDlg.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
progressDlg.setCancelable(false);
progressDlg.show();
}
@Override
protected Void doInBackground(String... strings) {
SharedPreferences pref = getSharedPreferences("Login", Context.MODE_PRIVATE);
String ip = pref.getString("ip", "");
String username = pref.getString("username", "");
String password = pref.getString("password", "");
String dbname = pref.getString("dbname", "");
rs = new ExecuteQuery().selectdata(ip, username, password, strings[0], dbname);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
if (rs != null) {
ArrayList<Ledger> ledgerlist = new ArrayList<>();
while (rs.next()) {
String accode = rs.getString("AC_CODE");
String acname = rs.getString("AC_NAME");
String acaddr = rs.getString("AC_ADD1");
String acmobile = rs.getString("AC_MOBILE");
String actype = rs.getString("AC_TYPE");
String amount = rs.getString("Amount");
String goldwt = rs.getString("GoldWt");
String silverwt = rs.getString("SilverWt");
ledgerlist.add(new Ledger(accode, acname, acaddr, acmobile, actype, amount, goldwt, silverwt));
}
LedgerAdapter adapter = new LedgerAdapter(LedgerActivity.this, ledgerlist);
rcv_ledger.setAdapter(adapter);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 69
Reputation: 2638
Looks like call to ResultSet.next() uses InputStream
under the hood to load row value or something like that.
So to avoid exception you need to return ArrayList<Ledger>
from doInBackground()
instead of parsing ResultSet
in onPostExecute()
Upvotes: 2