0

I've created a Python REST api with Flask, which 'should' allow me to use my phone as a Keyboard.

If I go to 'http://192.168.0.37:5000/keyboard/a' on my phones web browser, my laptop will enter the letter a. (This works).

I've tried to create an Android app that will make the http request, but it doesn't seem to work.

This is the code I used. I've tested it with in a basic Java file and it works, but it won't work in an Android Application.

private void sendGet(String letter) throws IOException {
    URL keyB = new URL("http://192.168.0.37:5000/keyboard/" + letter);
    Toast.makeText(getApplicationContext(), keyB.toString(), Toast.LENGTH_SHORT).show();

    URLConnection kb = keyB.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            kb.getInputStream(), "UTF-8"));

    String inputLine;
    StringBuilder a = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        a.append(inputLine);
    }
    in.close();

    Toast.makeText(getApplicationContext(), a.toString(), Toast.LENGTH_SHORT).show();
}
Dobz
  • 1,213
  • 1
  • 14
  • 36

1 Answers1

0

Thanks to @RemeesMSyde comment.

I put the method into an AsyncTask and it now works.

This is the code I have used.

class SendKeyStroke extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... letter) {
        try {
            URL keyB = new URL("http://192.168.0.37:5000/keyboard/" + letter[0]);
            System.out.println(keyB.toString());

            URLConnection kb = keyB.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(kb.getInputStream(), "UTF-8"));

            String inputLine;
            StringBuilder a = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                a.append(inputLine);
            }
            in.close();
        } catch(Exception ex) {

        }

        return null;
    }
}
Dobz
  • 1,213
  • 1
  • 14
  • 36