使用flutter时Http请求未得到响应



我对flutter还很陌生。我的任务是发送给我的注册链接的电子邮件、姓名和密码发送请求时的邮递员响应

注册链接的参考PHP代码

index.php
/**
* User Registration
* url - /register
* method - POST
* params - name, email, password
*/
$app->post('/register', function() use ($app) {
// check for required params
verifyRequiredParams(array('name', 'email', 'password'));
$response = array();
// reading post params
$name = $app->request->post('name');
$email = $app->request->post('email');
$password = $app->request->post('password');
// validating email address
validateEmail($email);
$db = new DbHandler();
$res = $db->createUser($name, $email, $password);
if ($res == USER_CREATED_SUCCESSFULLY) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
echoRespnse(201, $response);
} else if ($res == USER_CREATE_FAILED) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
echoRespnse(200, $response);
} else if ($res == USER_ALREADY_EXISTED) {
$response["error"] = true;
$response["message"] = "Sorry, this email already existed";
echoRespnse(200, $response);
}
});

发送数据的颤动代码是

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'newScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('User Registration Form')),
body: Center(
child: RegisterUser()
)
)
);
}
}
class RegisterUser extends StatefulWidget {
RegisterUserState createState() => RegisterUserState();
}
class RegisterUserState extends State {
// Boolean variable for CircularProgressIndicator.
bool visible = false ;
// Getting value from TextField widget.
final nameController = TextEditingController();
final emailController = TextEditingController();
final passwordController = TextEditingController();
Future userRegistration() async{
// Showing CircularProgressIndicator.
setState(() {
visible = true ; 
});
// Getting value from Controller
String name = nameController.text;
String email = emailController.text;
String password = passwordController.text;
// SERVER API URL
var url = 'http://cafeliant.com/android-login/signupApi.php';
var headers = {
'content-type': 'application/json'
};
// Store all data with Param Name.
var data = {'name': name, 'email': email, 'password' : password};
// Starting Web API Call.
var response = await http.post(url, body: json.encode(data),);
// Getting Server response into variable.
var message = jsonDecode(response.body);
print(message);
// If Web call Success than Hide the CircularProgressIndicator.
if(response.statusCode == 200){
setState(() {
visible = false; 
});
}
// Showing Alert Dialog with Response JSON Message.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("message"),
actions: <Widget>[
FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => newS(),));
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text('User Registration Form', 
style: TextStyle(fontSize: 21))),
Divider(),          
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: nameController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Name Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: emailController,
autocorrect: true,
decoration: InputDecoration(hintText: 'Enter Your Email Here'),
)
),
Container(
width: 280,
padding: EdgeInsets.all(10.0),
child: TextField(
controller: passwordController,
autocorrect: true,
obscureText: true,
decoration: InputDecoration(hintText: 'Enter Your Password Here'),
)
),
RaisedButton(
onPressed: userRegistration,
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Text('Click Here To Register User Online'),
),
Visibility(
visible: visible, 
child: Container(
margin: EdgeInsets.only(bottom: 30),
child: CircularProgressIndicator()
)
),
],
),
)));
}
}

从应用程序发送响应时,我的请求不会通过,打印响应时,会收到一个默认响应,表示我没有发送任何请求。

尝试在http.post方法中指定headers属性。下面是一个示例片段。

var response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(jsonData),
);

最后反序列化到Class对象。

相关内容

  • 没有找到相关文章

最新更新