简体   繁体   English

DioError [DioErrorType.RESPONSE]:Http 状态错误 [500],为什么?

[英]DioError [DioErrorType.RESPONSE]: Http status error [500], why?

I'm new to flutter, I use Dio in my project to make Members Register features.. but everytime I execute the register process, the Debug Console give me this error我是 flutter 的新手,我在我的项目中使用 Dio 来制作会员注册功能.. 但每次我执行注册过程时,调试控制台都会给我这个错误

I/flutter (13428): Instance of 'FormData'
I/flutter (13428): DioError [DioErrorType.RESPONSE]: Http status error [500]

however, when I tried to register with POSTMAN, It was a success.但是,当我尝试注册 POSTMAN 时,它是成功的。 So I'm not sure where the problem is.. here's my code, could you guys please help me?所以我不确定问题出在哪里..这是我的代码,你们能帮帮我吗? i've been stuck on this for weeks..我已经坚持了好几个星期了..

final String url = "https://api.censored.org/api/members/register";

Dio.FormData formData = Dio.FormData.fromMap({
  "nama_lengkap": name,
  "tempat_lahir": tempatlahir,
  "tanggal_lahir": selectedDate.toString(),
  "email": email,
  "password": password,
  "username": username,
  "nomor_ktp": noKTP,
  "alamat_ktp": alamatktpmember,
  "alamat_domisili": alamatmember,
  "pekerjaan": pekerjaanmember,
  "alamat_pekerjaan": alamatperusahaanmember,
  "no_whatsapp": noWAmember,
  "no_hp": noteleponmember,
  "nama_pemilik_rekening": namarekmember,
  "nomor_rekening": norekmember,
  "bank_id": _valBank,
  "aggrement": _eulargprogramming,

  //Foto
  "foto_ktp": await Dio.MultipartFile.fromFile(_fotoKtp.path),
  "selfie_dengan_ktp": await Dio.MultipartFile.fromFile(_fotoSelfie.path),
  "foto_kk": await Dio.MultipartFile.fromFile(_fotoKK.path),
  "foto_pengenal_lainnya":
      await Dio.MultipartFile.fromFile(_fotoKartu.path),
  "foto_buku_rekening":
      await Dio.MultipartFile.fromFile(_fotoRekening.path),
  "foto_profil": await Dio.MultipartFile.fromFile(_fotoSelfie.path),

  //Array
  "penjamin": {
    "stakeholder_id": _subrgProgramming1,
    "nama": penjaminNama,
    "nomor_ktp": penjaminNoktp,
    "alamat_ktp": penjaminAlamat,
    "alamat_domisili": penjaminDomisili,
    "no_hp": notelppenjamin,
  },
  "sosial_media": [
    {
      "sosial_media_id": _sosmedrgprogramming,
      "nama": sosialmediamember,
      "screenshot": await Dio.MultipartFile.fromFile(_fotoSosmed.path),
    }
  ],
  "saudara_kerabat": [
    {
      "stakeholder_id": 4,
      "nama": namasaudara,
      "no_hp": notelpsaudara,
    },
    {
      "stakeholder_id": 5,
      "nama": namakerabat1,
      "no_hp": notelpkerabat1,
    },
    {
      "stakeholder_id": 5,
      "nama": namakerabat2,
      "no_hp": notelpkerabat2,
    },
    {
      "stakeholder_id": 5,
      "nama": namakerabat3,
      "no_hp": notelpkerabat3,
    }
  ],
});
print(formData);
var res;
try {
  Dio.Dio doo = Dio.Dio();
  doo.options.headers['Accept'] = 'application/json';
  doo.options.contentType = 'application/json';
  doo.options.followRedirects = false;
  Dio.Response response = await doo.post(url, data: formData);

  if (response.statusCode == 200) {
    print(response.data);
    res = response;
    final data = jsonDecode(res.body);
    print(res);

    int regvalue = data['value'];
    String message = data['message'];
    if (regvalue == 1) {
      setState(() {
        Navigator.pop(context);
      });
      print(message);
      registerToast(message);
    } else if (regvalue == 2) {
      print(message);
      registerToast(message);
    } else {
      print(message);
      registerToast(message);
    }
  }
} on Dio.DioError catch (e) {
  if (e.response.statusCode == 422) {
    print(e.response.data);
  }
} on Dio.DioError catch (e) {
  if (e.response.statusCode == 500) {
    print(e.response.data);
  }
}


}

  registerToast(String toast) {
    return Fluttertoast.showToast(
        msg: toast,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white);
  }

this means your sever responded with InternalServerError but dio sees this as an exception to fix this either use try and catch blocs or pass this to your dio instace这意味着您的服务器以InternalServerError响应,但 dio 将此视为异常来解决此问题,可以使用 try 和 catch blocs 或将其传递给您的 dio instace

 final res = await dio.delete(
          url,
          data: postData,
          options: Options(

            followRedirects: false,
            // will not throw errors
            validateStatus: (status) => true,
            headers: headers,
          ),
        );


I had the same problem when I try send a image with Dio.当我尝试使用 Dio 发送图像时,我遇到了同样的问题。 But the problem only appears when i use the iphone emulator in a mac, in a android emulator all is ok, so I tried to starting the debug and compile my app in a real iphone, no emulator, and i didtn have this error.但是只有当我在 Mac 中使用 iphone 模拟器时才会出现问题,在 android 模拟器中一切正常,所以我尝试在真正的 iphone 中启动调试并编译我的应用程序,没有模拟器,我没有这个错误。 Maybe you have the same problem too, i hope help you.可能你也有同样的问题,希望能帮到你。

After Hours of struggle i found the way of uploading multiple images through dio packge here is my code you can use it accordingly经过数小时的努力,我找到了通过 dio packge 上传多个图像的方法,这是我的代码,您可以相应地使用它

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rent_house/screens/Admin/View_Property/AdminViwe.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http_parser/http_parser.dart';

var token;

Dio dio = Dio();
var apiURL = 'https://denga.r3therapeutic.com/public/api/addpost';
FormData formData = FormData();
Future<String> adminAddproperty({
  title,
  address,
  price,
  area,
  bedrooms,
  bathrooms,
  parking,
  other,
  description,
  List<File>? imageFileList,
  context,
}) async {
  List uploadList = [];
  for (var imageFiles in imageFileList!) {
    uploadList.add(await MultipartFile.fromFile(imageFiles.path,
        filename: imageFiles.path.split('/').last,
        contentType: MediaType('image', 'jpg')));
  }
  FormData formData = FormData.fromMap({
    'title': title,
    'address': address,
    'price': price,
    'area': area,
    'bedrooms': bedrooms,
    'bathrooms': bathrooms,
    'parking': parking,
    'others': "huhuhuhuh",
    'description': description,
    'images[]': uploadList,
  });
  SharedPreferences pref = await SharedPreferences.getInstance();
  token = pref.getString('token');
  print('AdminApisToken =$token');
  print('_images');

  Response responce;

    responce = await dio.post(
        apiURL ,
        data: formData,
        options: Options(headers: {
          HttpHeaders.authorizationHeader: "Bearer $token",
        }));
try{
  if(responce.data=="false"){
 Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => AdminProperty()),
    );
    Fluttertoast.showToast(
        msg: "Data Upload Successfull", backgroundColor: Colors.cyan);
  }
else{
      Fluttertoast.showToast(
        msg: "Wrong Input Data Type", backgroundColor: Colors.cyan);
}
    return '';
  } catch (e) {
    Fluttertoast.showToast(
        msg: "Internet Connection Error", backgroundColor: Colors.cyan);
    return '';
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法捕获 dioerror [dioerrortype.response]:http 状态错误 [statusCode] - Unable to catch dioerror [dioerrortype.response]: http status error [statusCode] I/flutter (28276):此处发现错误 DioError [DioErrorType.response]:Http 状态错误 [404] - I/flutter (28276): Error found Here DioError [DioErrorType.response]: Http status error [404] POST http:// localhost:8080 / server2_0_war_exploded /用户返回的响应状态为500 Internal Server Error - POST http://localhost:8080/server2_0_war_exploded/user returned a response status of 500 Internal Server Error 休眠状态:HTTP状态500-内部服务器错误 - Hibernate :HTTP Status 500 - Internal Server Error Json响应给出Http 500错误 - Json response giving Http 500 error JAX-RS响应HTTP状态500而不是HTTP状态400 - JAX-RS response with HTTP status 500 instead of HTTP status 400 使用Jquery和servlet检索图像会产生HTTP Status 500错误 - Retrieving images using Jquery and servlet produces HTTP Status 500 error HTTP状态代码500是否表示未出现其余错误代码 - Does HTTP Status Code 500 means that the rest error code are not occuring 使用REST模板和JSON响应格式在Spring MVC上返回的Http Status 500 - Http Status 500 returned on Spring MVC Using REST Templates and JSON Response Format flutter:异常 DioError [DioErrorType.DEFAULT]:类型“String”不是类型“Map”的子类型<string, dynamic> '</string,> - flutter: Exception DioError [DioErrorType.DEFAULT]: type 'String' is not a subtype of type 'Map<String, dynamic>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM