Guest User

Untitled

a guest
Jan 15th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. InetAddress.getByName causes NetworkOnMainThreadException
  2. currIP = InetAddress.getByName(currIPstr).toString().split("/")[1];
  3.  
  4. private String currStatIP = null;
  5. private String currentStatIPstr = null;
  6. private Integer currentStatIDstr = -1;
  7. private String currentPort = null;
  8. private String currentLocation = null;
  9. private String currUserName = null;
  10. private String currPassword = null;
  11. private Integer currStatID = -1;
  12. public void StatControl(Cursor c, final String[] commandtosend){
  13.  
  14.  
  15. /*the object will always start off with the first 4 fields being the current login information.
  16. * [0] = IP Address
  17. * [1] = port number
  18. * [2] = username
  19. * [3] = password
  20. * [4] = COMMAND -- this will be used in a switchcase inside the pingpong to decide what to do.
  21. * -- if the Socket is not open, the first 4 fields will be used to re-initiate the connection
  22. * -- otherwise, the command is drawn from this field and sent directly.
  23. * */
  24.  
  25.  
  26. currentStatIDstr = c.getInt(0);
  27. currentStatIPstr = c.getString(1);
  28. currentPort = c.getString(2);
  29. currentLocation = c.getString(3);
  30. currUserName = c.getString(4);
  31. currPassword = c.getString(5);
  32. currStatID = currentStatIDstr;
  33.  
  34.  
  35. Handler networkLookupHandler = new Handler();
  36. Runnable networkLookupRunnable = new Runnable() {
  37. public void run() {
  38. try {
  39. currStatIP = InetAddress.getByName(currentStatIPstr).toString().split("/")[1];
  40. } catch (UnknownHostException e) {
  41. e.printStackTrace();
  42. }
  43. int portNumber = 0;
  44. try {
  45. portNumber = Integer.parseInt(currentPort);
  46. } catch(NumberFormatException nfe) {
  47. nfe.printStackTrace();
  48. }
  49. String userName = currUserName;
  50. String passWord = currPassword;
  51. String[] command = commandtosend;
  52.  
  53. Object[] controlCommands = new Object[]{
  54. currStatID,
  55. currStatIP, //InetAddress of String representing the IP address
  56. portNumber, //int representation of the port number, taken from String
  57. currentLocation,
  58. userName,
  59. passWord,
  60. command
  61. };
  62. /*for(int i=0;i<controlCommands.length;i++){
  63. Log.d("object work","controlCommands[" + i + "] is " + controlCommands[i]);
  64. }*/
  65.  
  66. mainCommunicationThread(controlCommands);
  67. }
  68. };
  69. networkLookupHandler.post(networkLookupRunnable);
  70. }
  71.  
  72. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  73. StrictMode.setThreadPolicy(policy);
Add Comment
Please, Sign In to add comment