13

I am Using strapi.io open Source Node.js Headless CMS

this is my code for registerpage

void _registerUser() async {

http.Response response =
    await http.post('http://localhost:1337/auth/local/register', body: {
  "username": _userName,
  "email": _email,
  "password": _password,
});
final responseData = json.decode(response.body);

if (response.statusCode == 200) {
  // If the call to the server was successful, parse the JSON
  return print(responseData);
} else {
  // If that call was not successful, throw an error.
  throw Exception('Failed to load post');
}

}

any idea how to solve this

Problem about Socket exception

update:

Found the Solution: If you're running the server locally and using the Android emulator, then your server endpoint should be 10.0.2.2:8000 instead of localhost:8000 as AVD uses 10.0.2.2 as an alias to your host loopback interface (i.e) localhost

clear explanation checkout this Answer:

SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend

Thank you So much Everyone For Helping me!!❤❤❤

5

8 Answers 8

20

1 open cmd

2 run ipconfig at cmd

3 copy IPv4 Address from cmd

4 at 'http://localhost:1337/auth/local/register' replace 'localhost' with your IPv4 Address

5 run your app

Sign up to request clarification or add additional context in comments.

Comments

11

Instead of localhost, put your machine IP address. Mobile doesnt understand localhost.

Comments

4

Worked for me!! Insert your local IP address (192.168.x.x) instead of localhost. You can find it by writing ipconfig on Windows CMD and by writing ifconfig on Linux Terminal.

Comments

3

For MAC Users

Instead of localhost, put your MAC IP address. Mobile will not understand localhost.

Click

apple logo > system prefrences > Network

Under connected networks you can find your wifi ip

after that instead of

http://localhost:1337

you can use your wifi ip

http://192.168.2.250

enter image description here

Comments

1

just check if you have given the internet permission and the you have good internet connection

1 Comment

Yeah, I add Internet permission but still the Same Result and Even have a good internet connection !!
0

Use this library here for fetching data, And don't declare response as final and http.Response, instead of this you can use var

2 Comments

DioError (DioError [DioErrorType.DEFAULT]: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 51694)
Do you follow this ?
0

I had de same issue, it was caused because I builded an app version with signed on debug mode, on Android Studio, it was ok. But later on when I tried to run on the app on visual studio the error appears!!, so I went to Android Studio in normal mode, and runned the app , the error desapears. Because these error only appeeared in Android versión.

Comments

0

Normally I have found this problem in emulators when I need to connect a local server.

Usually I use localhost:port, but in an emulator you need to change localhost to 10.0.2.2 and it should works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.