Im new to programming for android (and programming in general) so sorry if this seems obvious. I am trying to connect to a database, and I understand that it has to be outside of the main thread but i thought that this is what my code was doing...apparently not because i keep getting the error: android.os.NetworkOnMainThreadException
What am i doing wrong?
package com.example.dbtesting;
import java.sql.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
TextView myTextView = (TextView)findViewById(R.id.result);
myTextView.setText(msg.getData().getString("name"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view){
Runnable runnable = new Runnable() {
@Override
public void run() {
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://URL:1433/DBNAME", "myUSER", "myPASS");
System.out.println("connected");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("SELECT * FROM People");
Bundle bundle = new Bundle();
//Print the data to the console
String gah = "";
while(reset.next()){
gah += reset.getString(2) + "\n";
}
conn.close();
Message msg = handler.obtainMessage();
bundle.putString("myKey", gah);
msg.setData(bundle);
handler.sendMessage(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Thread myThread = new Thread(runnable);
myThread.run();
}
}
What am i doing wrong?
package com.example.dbtesting;
import java.sql.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
TextView myTextView = (TextView)findViewById(R.id.result);
myTextView.setText(msg.getData().getString("name"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view){
Runnable runnable = new Runnable() {
@Override
public void run() {
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://URL:1433/DBNAME", "myUSER", "myPASS");
System.out.println("connected");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("SELECT * FROM People");
Bundle bundle = new Bundle();
//Print the data to the console
String gah = "";
while(reset.next()){
gah += reset.getString(2) + "\n";
}
conn.close();
Message msg = handler.obtainMessage();
bundle.putString("myKey", gah);
msg.setData(bundle);
handler.sendMessage(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Thread myThread = new Thread(runnable);
myThread.run();
}
}