android.os.NetworkOnMainThreadException

Olá. Eu estou recebendo na minha aplicação essa exceção aí do título. Bem, é meio óbvio. É tipo o JavaFX que precisa daquela estrutura de Platform.runLater por que não dá pra fazer em outra Thread. Só que no caso, pelo que eu entendi, eu devo usar outra Thread para não enterromper a Activity. Porém eu não estou vendo como fazer isso no meu código atual.

MainActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;

import java.sql.SQLException;

public class MainActivity extends AppCompatActivity {

    public TextView money = null;
    public ListView history = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        money = (TextView) findViewById(R.id.money);
        history = (ListView) findViewById(R.id.history);
        try {
            money.setText("R$ " + GlobalDAO.getBox());
            System.out.println("R$ "+GlobalDAO.getBox());
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, GlobalDAO.getReserveds());
            System.out.println("Itens: "+GlobalDAO.getReserveds().size());
           history.setAdapter(adapter);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void startPolling() throws SQLException {
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, GlobalDAO.getReserveds());
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        money.setText("R$ " + GlobalDAO.getBox());
                        history.setAdapter(adapter);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void add(View v) {
        Intent intent = new Intent(this, Add.class);
        startActivity(intent);
    }

    public void rm(View v) {
        Intent intent = new Intent(this, Rm.class);
        startActivity(intent);
    }
}

GlobalDAO

import com.google.gson.Gson;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by thall on 24/03/2017.
 */

public class GlobalDAO {

    public static String getBox() throws SQLException {
        InputStream is = null;
        String line = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httppost = new HttpPost("http://192.168.1.102/app_formatura/get_money.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            return result;
        } catch (Exception e) {
            return "";
        }
    }

    public static ArrayList<String> getReserveds() throws SQLException {
        ArrayList<String> toReturn = new ArrayList<String>();
        InputStream is = null;
        String line = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httppost = new HttpPost("http://192.168.1.102/app_formatura/get_reserveds.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            Gson gson = new Gson();
            Reserved r = gson.fromJson(result, Reserved.class);
            if(!r.isAdquirido()){
                toReturn.add("Para adquirir "+r.getNome()+", custa "+r.getCusto());
                System.out.println("Para adquirir "+r.getNome()+", custa "+r.getCusto());
            }else{
                toReturn.add(r.getNome()+", custa "+r.getCusto()+", adquirido");
                System.out.println(r.getNome()+", custa "+r.getCusto()+", adquirido");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toReturn;
    }

    public static String[] getSpinnerContent() throws SQLException {
        InputStream is = null;
        String line = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httppost = new HttpPost("http://192.168.1.102/app_formatura/get_spinner_content.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            System.out.println(result);
            return result.split(",");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Retornou uma exceção");
        }
        return null;
    }

    public static boolean save_Source(boolean kind, String name, String name2, double gived, String buyer, int count) throws SQLException {
        InputStream is = null;
        String line = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httppost = new HttpPost("http://192.168.1.102/app_formatura/save_source.php?kind="+kind+"&name="+name+
            "&name2="+name2+"&gived="+gived+"&buyer="+buyer+"&count"+count);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            return Boolean.parseBoolean(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean save_decrement(String name, boolean buyed, double cost, int count) throws SQLException {
        InputStream is = null;
        String line = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httppost = new HttpPost("http://192.168.1.102/app_formatura/save_decrement.php?name="+name+
                    "&buyed="+buyed+"&cost="+cost+"&count"+count);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            return Boolean.parseBoolean(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}

Alguém pode me ajudar?

Cara, tá difícil pesquisar ou é preguiça mesmo?
Só peguei a mensagem da exceção e joguei no google, tem muitas respostas prontas.
Um exemplo do próprio guj.

Perdão. Eu jamais vou criar um tópico aqui sem ter feito pelo menos uma pesquisa :stuck_out_tongue:. Eu tô com a cabeça meio pilhada aqui, por isso não tô conseguindo. Vou tentar mais. Obrigado pela atenção.

Consegui resolver assim:

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                //código pra executar
            }
});