0

I am trying to get data from database using mysql-php and insert them into a ListView- android 1-There is one title in database and even if I remove the while loop in php file I got the error below 2-How to handle if I have multiple results?

Here's my code

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_polls);
        ListView listView=(ListView) findViewById(R.id.list_polls);
        ArrayList<String> titles=new ArrayList<>();
        ArrayAdapter<String> a=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles );
        listView.setAdapter(a);
        Handler handler=new Handler(this.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    titles.add(getPolls());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

and this is my method to connect with mysql

public String getPolls() throws IOException {
        URL url = new URL("http://192.168.1.8/getPolls.php");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        InputStream in = con.getInputStream();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        String line = r.readLine();
        return line;
    }

Here is my php code:

<?php
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "evote");

$qu="select * from polls";
if ($result=mysqli_query($con,$qu)) {
    if (mysqli_num_rows($result)>0) {
            while ($row=mysqli_fetch_array($result)) {
                echo $row['title'];
            }
        }
    }
?>

And here is the error I am getting:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.evote, PID: 21601 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.evote/com.example.evote.userPolls}: android.os.NetworkOnMainThreadException

And it also shows me an error on line of

InputStream in = con.getInputStream();
Mah
  • 21
  • 4
  • In android, one cannot do network activity on the main (or UI) thread - it must be done in a separate thread. See this android info on background work: https://developer.android.com/guide/background . See, impersistent work - the `ExecutorService` is likely where you'll end up. Also many popular answers in SO for android are outdated - solutions for NetworkOnMainThreadException is an example - AsyncTasks are deprecated so avoid the most popular answer when searching. Note `handler.post` defers the work but still runs on UI/main thread given you are using the `MainLooper` - hence the error. – Computable May 19 '22 at 16:20
  • I tried to create a HandlerThread and then create a Handler to get his Looper instead og getMainLooper but I got the same result.. do you have any example of how use `ExecutorService` in this case? – Mah May 19 '22 at 16:27
  • I noticed from your edit history where you create a `HandlerThread` but you did not `.start()` it ? See https://stackoverflow.com/a/18844161/17856705 - I would try that first. Otherwise the getLooper on the handler thread would not work as expected – Computable May 19 '22 at 16:34
  • Aside from that, this https://stackoverflow.com/a/68522179/17856705 is a starting point on `ExecutorService`. – Computable May 19 '22 at 16:41
  • Whoever dup'ed the Q should note that the most popular A uses a deprecated solution. And there are 65 alternate answers for someone to dig through. Not too helpful. – Computable May 19 '22 at 16:46
  • 1
    The 'ExecutetorService` has worked :D , thank you very much, and yes all answers are about AsyncTasks.. Idk why the question is closed. – Mah May 19 '22 at 17:24

0 Answers0