-2

I created a class TFTPClient which allows user to connect to the server and able to make certain request through the class methods. I have created and tested this outside of Android Studio, but when I transferred and compiled my code I get an error. The error is caused in the MainActivity.java but not when creating the class object, but calling certain method

TFTPClient c = new TFTPClient(); //Works fine

try {
    c.sendAndReceive(); //Get the error
} catch (UnknownHostException e) {
    e.printStackTrace();
}

Here is the sendAndReceive method code

public void sendAndReceive() throws UnknownHostException {
    int sendPort;
    sendPort = SERVER_RECV_PORT; //(60700)
    int clientConnectionPort = -1;
    InetAddress serverAddress = InetAddress.getByName("172.17.49.153");

    try {
        clientConnectionPort = sendRequest(serverAddress, sendPort, "all");
    }
    catch (IOException e) {
        //System.out.println("error");
    }
    receive(serverAddress, clientConnectionPort);
}
Cœur
  • 32,421
  • 21
  • 173
  • 232
AyeJay
  • 359
  • 1
  • 2
  • 9

1 Answers1

-1

Did you make sure to request the internet permission? According to Finley Smith, in older versions of android it doesn't let you make access the internet without permission. You can add the internet permission by putting this line of code in the AndroidManifest file:

<uses-permission android:name="android.permission.INTERNET" />

Also, could you send the error message from the LogCat so there is more information to go off of next time, Thank you!

  • You're wrong. [You still need the internet permission](https://developer.android.com/training/basics/network-ops/connecting.html). Even the newest docs state that. [The docs on the internet permission says it isn't deprecated](https://developer.android.com/reference/android/Manifest.permission.html#INTERNET) meaning it still is required. Permissions that aren't used in later versions are deprecated, but not removed to maintain backward compatibility. You need the internet permission either way, it doesn't matter what version – Zoe Oct 28 '17 at 19:39
  • It would still be best if we knew what the error was exactly. – Yasser Arguelles Oct 28 '17 at 19:42
  • You can do that through a comment when you have enough reputation. In the meanwhile, make sure you don't rely too much on answers when even the source points to Google Play and not the Android OS itself. Right now this is an answer with wrong information, the question is a duplicate and off-topic because it has no MCVE. Instead of essentially adding a glorified comment as an answer where there's one part that makes it wrong – Zoe Oct 28 '17 at 19:44
  • I will keep this in mind for the future, thank you! – Yasser Arguelles Oct 28 '17 at 19:45