Android 模拟器中的 Socket 异常错误

问题描述 投票:0回答:0

我是 laravel 和 flutter 的新手,所以我成功地配置了我的 laravel 后端。我使用 postman 测试 API 并成功发布到数据库,现在当我尝试使用 android 模拟器发布数据时,我一直收到错误 (SocketException.connection refused (OS error.connection refused, ernno=111) address= 127.0. 0.1,端口不断变化)

我的API客户端如下

import 'package:myapp/utils/app_constants.dart';
import 'package:get/get.dart';

class ApiClient extends GetConnect implements GetxService{
  late String token;
  final String appBaseUrl;

  late Map<String, String> _mainHeaders;

  ApiClient({required this.appBaseUrl}){
    baseUrl = appBaseUrl;
    timeout = const Duration(seconds: 30);
    token = AppConstants.TOKEN;
    _mainHeaders ={
      'Content-type':'application/json; charset=UTF-8',
      'Authorization': 'Bearer $token',
    };
  }

  void updateHeader(String token){
    _mainHeaders ={
      'Content-type':'application/json; charset=UTF-8',
      'Authorization': 'Bearer $token',
    };

  }
  Future<Response> getData(String uri,)async{
    try{
      Response response =await get(uri);
      return response;
    }catch(e){
      return Response(statusCode: 1, statusText: e.toString());
    }
  }
  Future<Response>postData(String uri, dynamic body) async {
    print(body.toString());
    try{
      Response response = await post(uri, body, headers: _mainHeaders);
      return response;
    }catch(e){
      print(e.toString());
      return Response (statusCode: 1, statusText: e.toString());
    }
  }
}

AppConstants.dart

class AppConstants{
  static const String APP_NAME ="RideSharing";
  static const int APP_VERSION = 1;

  static const String BASE_URL ="http://127.0.0.1:8000/";

  //auth end points
  static const String REGISTRATION_URI ="api/dv/auth/register";

  static const String TOKEN = "RideToken";

}

error screenshot

flutter apache dart laravel-8
© www.soinside.com 2019 - 2024. All rights reserved.