问题描述
我正在尝试获取由android studio开发的flutter应用程序,以连接到本地托管的C#API.在IIS Express上运行api会得到 https://localhost:44364/api/trip 的网址,我认为与使用Android Studio中的 http://10.0.2.2:44364/api 有关模拟器.
I'm attempting to get a flutter app, made in android studio, to connect to a c# api hosted locally. Running the api on IIS Express gives a url of https://localhost:44364/api/trip, which I believe relates to http://10.0.2.2:44364/api in Android studio when using the emulator.
我正在尝试通过API从数据库中获取行程列表.邮递员工作得很好.
I'm attempting to retrieve a list of Trips from the db via API. Postman works fine.
我遇到多个错误.我尝试了模拟器,也尝试了直接插入电话并将地址切换到我的IP地址.我什至尝试将api切换为在控制台而不是IIS中运行.什么都行不通.
I get multiple errors. I have tried the emulator, I've also tried plugging a phone in directly and switching the address to my ip address. I've even tried switching the api to run in the console and not IIS. Nothing works.
我确实尝试通过模拟器浏览器访问api,但被告知该站点不安全,导致我认为这是证书问题.
I did attempt to reach the api through the emulator browser and was told the site was unsafe, leading me to think it's a certificate problem.
flutter项目非常简单,处于起步阶段,我仍在学习如何对其进行编程.
The flutter project is fairly simple, it's in its infancy stage and I'm still learning how to program it.
这是三个flutter文件以及错误列表.
Here are the three flutter files, and the error list.
main.dart
main.dart
import 'dart:convert';
import 'package:bike_client/API.dart';
import 'package:bike_client/models/Trip.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Track It',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
createState() => new _MyHomePageState();
}
class _MyHomePageState extends State {
var trips = new List<Trip>();
_getTrips() {
API.getTrips().then((response) {
setState(() {
Iterable list = json.decode(response.body);
trips = list.map((model) => Trip.fromJson(model)).toList();
});
});
}
initState() {
super.initState();
_getTrips();
}
dispose() {
super.dispose();
}
@override
build(context) {
return Scaffold(
appBar: AppBar(
title: Text("Trip List"),
),
body: ListView.builder(
itemCount: trips.length,
itemBuilder: (context, index) {
return ListTile(title: Text((trips[index].tripId).toString()));
},
));
}
}
API.dart
import 'dart:async';
import 'package:http/http.dart' as http;
//const baseUrl = "https://192.168.1.14:44364/api";
const baseUrl = "http://10.0.2.2:44364/api";
class API {
static Future getTrips() {
var url = baseUrl + "/trip/1";
return http.get(url);
}
}
Trip.dart
Trip.dart
class Trip {
int tripId;
int userId;
Trip(int tripId, int userId) {
this.tripId = tripId;
this.userId = userId;
}
Trip.fromJson(Map json)
: tripId = json['tripId'],
userId = json['userId'];
Map toJson() {
return {'tripId': tripId, 'userId': userId};
}
}
控制台
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38612
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38760
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38884
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException:
AND MUCH MORE!
推荐答案
该错误主要是在您的android AVD或android设备未连接到互联网时发生的.有时,开发人员尝试从android设备而非AVD访问localhost上的插件,在这种情况下,会发生相同的错误.我希望这能解决您的问题.
This error occurs mostly when your android AVD or android device is not connected to internet. Sometimes, the developer tries to access plugin on localhost from android device instead of AVD, in such cases, same error occurs. I hope this solves your problem.
其他情况包括服务器端防火墙阻止了端口.
Other scenarios involves port blocked by firewalls on serverside.
如果您认为我的回答是有用的,请进行投票.
Kindly upvote if you find my answer usable.
这篇关于Flutter未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!