Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- InetAddress.getByName causes NetworkOnMainThreadException
- currIP = InetAddress.getByName(currIPstr).toString().split("/")[1];
- private String currStatIP = null;
- private String currentStatIPstr = null;
- private Integer currentStatIDstr = -1;
- private String currentPort = null;
- private String currentLocation = null;
- private String currUserName = null;
- private String currPassword = null;
- private Integer currStatID = -1;
- public void StatControl(Cursor c, final String[] commandtosend){
- /*the object will always start off with the first 4 fields being the current login information.
- * [0] = IP Address
- * [1] = port number
- * [2] = username
- * [3] = password
- * [4] = COMMAND -- this will be used in a switchcase inside the pingpong to decide what to do.
- * -- if the Socket is not open, the first 4 fields will be used to re-initiate the connection
- * -- otherwise, the command is drawn from this field and sent directly.
- * */
- currentStatIDstr = c.getInt(0);
- currentStatIPstr = c.getString(1);
- currentPort = c.getString(2);
- currentLocation = c.getString(3);
- currUserName = c.getString(4);
- currPassword = c.getString(5);
- currStatID = currentStatIDstr;
- Handler networkLookupHandler = new Handler();
- Runnable networkLookupRunnable = new Runnable() {
- public void run() {
- try {
- currStatIP = InetAddress.getByName(currentStatIPstr).toString().split("/")[1];
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- int portNumber = 0;
- try {
- portNumber = Integer.parseInt(currentPort);
- } catch(NumberFormatException nfe) {
- nfe.printStackTrace();
- }
- String userName = currUserName;
- String passWord = currPassword;
- String[] command = commandtosend;
- Object[] controlCommands = new Object[]{
- currStatID,
- currStatIP, //InetAddress of String representing the IP address
- portNumber, //int representation of the port number, taken from String
- currentLocation,
- userName,
- passWord,
- command
- };
- /*for(int i=0;i<controlCommands.length;i++){
- Log.d("object work","controlCommands[" + i + "] is " + controlCommands[i]);
- }*/
- mainCommunicationThread(controlCommands);
- }
- };
- networkLookupHandler.post(networkLookupRunnable);
- }
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
Add Comment
Please, Sign In to add comment