Im having issues with Sockets and Threads in API 25. Im directly targeting API 25 on my Nexus 6p with 7.1.1
When trying to communicate with my node server on port 8124 I initialize a new thread and initialize a socket but I either get a networkonmainthreadexception
public class MainActivity extends AppCompatActivity {
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Button LoginButton = (Button) findViewById(R.id.button2);
LoginButton.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
TryLogin(v);
}
}
);
}
public void TryLogin(View v){
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket("192.168.1.57", 8124);
Log.i("SOCKET", "Was the thread started?");
} catch(UnknownHostException e1){
} catch (IOException e1){
}
}
}).start();
}
}
When I call start() I never see "Was the thread started?". Neither is any error thrown in logcat. But when I change to run() i get networkonmainthreadexception error. It seams as if something was changed. Ive verified that tcp connections to my node server show up using a node client.