Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

flutter - Impossible to get answer from local http server

I wrote simple http server that accept get request and return data. Data are accessable from browser.

The problem with Flutter app. I can't get result in: http.get().then when I am trying to get access to my local web server. But When I am changing it to public it's work.

enter image description here enter image description here

Code:

class HomeView extends StatelessWidget {
  @override 
  build(Build) {
    return Column(
      children: [
      WorkersSlider(),
      RaisedButton(onPressed: () =>  {
      
      print("sdfsfd"),
      http.get('https://my-json-server.typicode.com/typicode/demo/posts',
      // http.get('http://127.0.0.1:5000/status',
        headers: {
          "Accept": "application/json",
          "Access-Control_Allow_Origin": "*"
        }
      ).then((value) => {
        print("1111113"),
        print("value from parser: $value")
      }),
      
      } ),

      
    ],);
  }

}

server code:

main() async {
  const int PORT = 5000;
  var app = express();
  app.use(BodyParser.json());

  app.get('/status', (req, res) async {
      print("status request");
      List jobsAsList = [];
      for(var job in jobsList) 
      {
        // jobs generation logic is skipped
        jobsAsList.add(job.toJson());
      }
      res.json({'data': jobsAsList});

  });

  app.listen(port: PORT, cb: (int port) => print('Listening on port $port'));

}

curl works:

curl -X GET http://127.0.0.1:5000/status
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   279    0   279    0     0   272k      0 --:--:-- --:--:-- --:--:--  272k{"data":[{"jobPid":13772,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"},{"jobPid":8544,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"},{"jobPid":18156,"job_cmd":null,"jobStatus":null,"lastPingDate":"2021-01-25T14:53:47.000"}]}

Any ideas?

UPD: https://github.com/dart-lang/http/issues/433

question from:https://stackoverflow.com/questions/65885624/impossible-to-get-answer-from-local-http-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Im editing my answer, I didn't understood your problem at first.

If you're using a Android Emulator to test this code, this is probably a problem with your localhost.

The localhost of your PC, isn't the same localhost as your emulator since Android Emulator run in Virtual Machines.

Try editing your endpoint URL to http://10.0.2.2:5000/status


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...