• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have java code that sends strings via ip to a python script. The code works perfectly with the emulator but when I successfully install the app via usb to my phone it does not work. Here is the code:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {
    public String message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button btn_python = findViewById(R.id.python);
        final Button btn_movie = findViewById(R.id.movie);
        final Button btn_hw = findViewById(R.id.homework);
        btn_python.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                send py = new send();
                message = "python";
                Log.i("Button", "Button works");
                System.out.println("whatever");
                py.execute();
            }
        });
        btn_movie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view2) {
                send mov = new send();
                message = "movie";
                mov.execute();
            }
        });
        btn_hw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view2) {
                send hw = new send();
                message = "homework";
                hw.execute();
            }
        });
    }
    class send extends AsyncTask<Void,Void,Void>{
        Socket s;
        PrintWriter pw;
        @Override
        protected Void doInBackground(Void...params) {
            System.out.println("whatevernumbertwo");
            try {
                System.out.println("whatevernumberthree");
                s = new Socket("ip address", 7800);
                Log.i("Socket", "connects to socket");
                pw = new PrintWriter(s.getOutputStream());
                Log.i("output stream", "Output stream works");
                pw.write(message);
                Log.i("write", "Write works");
                pw.flush();
                Log.i("flush", "Flush works");
                pw.close();
                s.close();
            } catch (UnknownHostException e) {
                System.out.println("Fail");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Fail");
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }  
            return null;
        }
    }
}

As I mentioned this works on the emulator but not on the actual device. The proper permissions have also been given. What am I doing wrong? Thanks in advance.

After much digging around, it turned out to be the server's firewall all along. That explains why (apparently) no exception was thrown, and why the code didn't seem to execute; it was executing, it was just getting stuck inside Socket() (during the connect).

Surely Socket() is, in fact, throwing an IOException; it probably just takes a while.

The code works on the emulator because, as it is operating on the same machine, it is behind the firewall.

  • 1
Reply Report