1. Download our Official Android App: Forums for Android!

Apps Exiting Main Thread

Discussion in 'Android Development' started by ducknoise, Feb 7, 2014.

  1. ducknoise

    ducknoise Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Feb 7, 2014

    Feb 7, 2014
    1
    0
    5
    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();

    }
    }
     

    Advertisement

  2. AhamedM

    AhamedM Lurker
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Feb 8, 2014

    Feb 8, 2014
    3
    0
    5
    You are accessing network on main thread... Use AsyncTask for network operations... If you dont want to use AsyncTask Add this to onCreate method "StrictMode.enableDefaults();" But I wont recommend that...
     

Share This Page

Loading...