Commit 3f3158ca authored by jinzhu's avatar jinzhu

初步完成网络解析

parent 6e37fe57
import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/model/message/replied_content.dart'; import 'package:gmalpha_flutter/model/message/replied_content.dart';
class Message { class Message {
final String userId; final int userId;
final String name; final String name;
final String icon; final String icon;
final String time; final num time;
final String content; final String content;
final String id; final int id;
// final String repliedContent; // final String repliedContent;
final RepliedContent repliedContent; final RepliedContent repliedContent;
......
import 'replied_content.dart'; import 'replied_content.dart';
class RepliedContent { class RepliedContent {
final String id; final int id;
final String type; final int type;
final String topicId; final int topicId;
final String content; final String content;
final String contentType; final int contentType;
RepliedContent({this.id, this.type, this.topicId, this.content, this.contentType}); RepliedContent({this.id, this.type, this.topicId, this.content, this.contentType});
......
...@@ -179,6 +179,11 @@ class DioUtil { ...@@ -179,6 +179,11 @@ class DioUtil {
if (_dio != null) { if (_dio != null) {
_dio.options = _options; _dio.options = _options;
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
client.findProxy = (url) {
return _isDebug ? 'PROXY $_proxy' : 'DIRECT';
};
};
if (_pem != null) { if (_pem != null) {
// httpClientAdapter // httpClientAdapter
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
...@@ -190,11 +195,7 @@ class DioUtil { ...@@ -190,11 +195,7 @@ class DioUtil {
} }
return false; return false;
}; };
client.findProxy = (url) {
return _isDebug ? 'PROXY $_proxy' : 'DIRECT';
};
}; };
} }
if (_pKCSPath != null) { if (_pKCSPath != null) {
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
...@@ -213,11 +214,15 @@ class DioUtil { ...@@ -213,11 +214,15 @@ class DioUtil {
/// [path] The url path. /// [path] The url path.
/// [data] The request data /// [data] The request data
/// [options] The request options. /// [options] The request options.
/// Map<String, dynamic> queryParameters,
/// <BaseResp<T> 返回 status code msg data . /// <BaseResp<T> 返回 status code msg data .
Future<BaseResp<T>> request<T>(String method, String path, Future<BaseResp<T>> request<T>(String method, String path,
{data, Options options, CancelToken cancelToken}) async { {Map<String, dynamic> queryParameters, data, Options options, CancelToken cancelToken}) async {
// _dio.request(path, que)
// _dio.request(path, )
Response response = await _dio.request(path, Response response = await _dio.request(path,
data: data, data: data,
queryParameters: queryParameters,
options: _checkOptions(method, options), options: _checkOptions(method, options),
cancelToken: cancelToken); cancelToken: cancelToken);
_printHttpLog(response); _printHttpLog(response);
...@@ -233,10 +238,6 @@ class DioUtil { ...@@ -233,10 +238,6 @@ class DioUtil {
response.statusCode == HttpStatus.created) { response.statusCode == HttpStatus.created) {
try { try {
if (response.data is Map) { if (response.data is Map) {
// _status = (response.data[_statusKey] is int)
// ? response.data[_statusKey].toString()
// : response.data[_statusKey];
_code = (response.data[_codeKey] is String) _code = (response.data[_codeKey] is String)
? int.tryParse(response.data[_codeKey]) ? int.tryParse(response.data[_codeKey])
: response.data[_codeKey]; : response.data[_codeKey];
...@@ -261,10 +262,8 @@ class DioUtil { ...@@ -261,10 +262,8 @@ class DioUtil {
type: DioErrorType.RESPONSE, type: DioErrorType.RESPONSE,
)); ));
} }
} else { }
_code = 1;
_msg = '请求失败';
}
return new Future.error(new DioError( return new Future.error(new DioError(
response: response, response: response,
message: "statusCode: $response.statusCode, service error", message: "statusCode: $response.statusCode, service error",
...@@ -279,9 +278,11 @@ class DioUtil { ...@@ -279,9 +278,11 @@ class DioUtil {
/// [options] The request options. /// [options] The request options.
/// <BaseRespR<T> 返回 status code msg data Response. /// <BaseRespR<T> 返回 status code msg data Response.
Future<BaseRespR<T>> requestR<T>(String method, String path, Future<BaseRespR<T>> requestR<T>(String method, String path,
{data, Options options, CancelToken cancelToken}) async { {Map<String, dynamic> queryParameters, data, Options options, CancelToken cancelToken}) async {
// _dio.request(path,queryParameters: )
Response response = await _dio.request(path, Response response = await _dio.request(path,
data: data, data: data,
queryParameters: queryParameters,
options: _checkOptions(method, options), options: _checkOptions(method, options),
cancelToken: cancelToken); cancelToken: cancelToken);
_printHttpLog(response); _printHttpLog(response);
...@@ -444,8 +445,8 @@ class DioUtil { ...@@ -444,8 +445,8 @@ class DioUtil {
options.connectTimeout = 10 * 1000; options.connectTimeout = 10 * 1000;
options.receiveTimeout = 20 * 1000; options.receiveTimeout = 20 * 1000;
options.contentType = ContentType.parse('application/x-www-form-urlencoded'); options.contentType = ContentType.parse('application/x-www-form-urlencoded');
// options.baseUrl = 'https://earth.iyanzhi.com/';http://earth.gmapp.env/ options.baseUrl = 'https://earth.iyanzhi.com/';
options.baseUrl = 'http://earth.gmapp.env/'; // options.baseUrl = 'http://earth.gmapp.env/';
Map<String, dynamic> headers = Map<String, dynamic>(); Map<String, dynamic> headers = Map<String, dynamic>();
headers['Accept'] = 'application/json'; headers['Accept'] = 'application/json';
return options; return options;
......
...@@ -29,42 +29,22 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli ...@@ -29,42 +29,22 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli
Future<List<Message>> _fetchMesssageList() async { Future<List<Message>> _fetchMesssageList() async {
BaseOptions options = DioUtil.getDefOptions(); BaseOptions options = DioUtil.getDefOptions();
options.baseUrl = "http://earth.gmapp.env/"; Map<String, dynamic> cookie = {'cookie': '_gm_token=4cabd51562739648; _gtid=fac513b6a2bf11e9acfd525400e5c7a38141; sessionid=7j7s902fmro0pvybwrp374khvva6l3xf;'};
final cookie = new Map<String, dynamic>.from({'_gm_token': 'dfb1d61562677661', '_gtid': 'e49a16069e4411e98014525400e82fab836', 'sessionid': '4w1pyiayin5odywgbosemgul8m28t6x4'}); HttpConfig config = new HttpConfig(options: options, nativeCookie:cookie);
HttpConfig config = new HttpConfig(options: options, nativeCookie: cookie);
DioUtil().setConfig(config); DioUtil().setConfig(config);
print(cookie);
List<Message> messageList = List<Message>(); List<Message> messageList = List<Message>();
print('aaaaa'); print('aaaaa');
DioUtil().requestR(Method.get, "api/v1/reply/my",data: {'page': 1, 'count': 10}).then((res){ BaseResp<List> baseresp = await DioUtil().request<List>(Method.get, "api/v1/reply/my", queryParameters: {'page': 1, 'count': 10});
print('333333333'); if (baseresp.code != 0) {
print(res); return new Future.error(baseresp.msg);
return messageList; }
if (baseresp.data != null) {
messageList = baseresp.data.map((value) {
return Message.fromJson(value);
}).toList();
} }
).then((error) {
print('ddddd');
print(error);
// return messageList;
});
// print(res);
return messageList; return messageList;
// .then((res){
// if (res.code == 0) {
// Map<String, dynamic> data = json.decode(res.data);
// for (dynamic data in data['data']) {
// Message messageData = Message.fromJson(data);
// messageList.add(messageData);
// }
// }
// return messageList;
// }).then((error){
// return messageList;
// });
}
_getdata() async{
return _fetchMesssageList();
} }
@override @override
...@@ -75,32 +55,30 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli ...@@ -75,32 +55,30 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli
), ),
floatingActionButton: new FloatingActionButton( floatingActionButton: new FloatingActionButton(
onPressed: () { onPressed: () {
List<Message> res = _getdata(); },
},
child: new Icon(Icons.add_box), child: new Icon(Icons.add_box),
elevation: 3.0, elevation: 3.0,
highlightElevation: 2.0, highlightElevation: 2.0,
backgroundColor: Colors.red, // 红色 backgroundColor: Colors.red, // 红色
), ),
// body: ListView(children: _getListData()), // body: ListView(children: _getListData()),
body: new Center( body: new Center(
// child: FutureBuilder( child: FutureBuilder(
// future: _fetchMesssageList(), future: _fetchMesssageList(),
// builder: (context, snapshot) { builder: (context, snapshot) {
// switch (snapshot.connectionState) { switch (snapshot.connectionState) {
// case ConnectionState.none: case ConnectionState.none:
// case ConnectionState.waiting: case ConnectionState.waiting:
// break; break;
// default: default:
// if (snapshot.hasData) { if (snapshot.hasData) {
// return _createListView(context, snapshot); return _createListView(context, snapshot);
// } }
// } }
},
// },
), ),
// ) )
); );
} }
...@@ -108,6 +86,7 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli ...@@ -108,6 +86,7 @@ class _MessageHomePageState extends State<MessageHomePage> with AutomaticKeepAli
Widget _createListView(BuildContext context, AsyncSnapshot snapshot) { Widget _createListView(BuildContext context, AsyncSnapshot snapshot) {
List<Message> messageList = snapshot.data; List<Message> messageList = snapshot.data;
return ListView.builder( return ListView.builder(
shrinkWrap: true,
key: new PageStorageKey('message-list'), key: new PageStorageKey('message-list'),
itemCount: messageList.length, itemCount: messageList.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
......
...@@ -29,14 +29,14 @@ class MessageItem extends StatelessWidget { ...@@ -29,14 +29,14 @@ class MessageItem extends StatelessWidget {
), ),
); );
var titleRow = new Row( var titleRow = new Row (
children: <Widget>[ children: <Widget>[
new Row ( new Row (
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Text(message.name, style: TextStyle(color: Color(0xff323232), fontWeight: FontWeight.bold, fontSize: 13)), Text(message.name, style: TextStyle(color: Color(0xff323232), fontWeight: FontWeight.bold, fontSize: 13)),
new Padding ( new Padding (
padding: EdgeInsets.only(left: 8), padding: EdgeInsets.only(left: 8, right: 8),
child: Text('评论了你', style: TextStyle(color: Color(0xff323232), fontSize: 13)), child: Text('评论了你', style: TextStyle(color: Color(0xff323232), fontSize: 13)),
) )
], ],
...@@ -44,16 +44,23 @@ class MessageItem extends StatelessWidget { ...@@ -44,16 +44,23 @@ class MessageItem extends StatelessWidget {
], ],
); );
var contenRow = new Row( timeStr() {
mainAxisAlignment: MainAxisAlignment.spaceBetween, datefr
}
var contenRow = new Row (
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
new Row ( new Expanded(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ child: new Text(message.content, maxLines: 2, textAlign: TextAlign.start, overflow: TextOverflow.ellipsis, style: TextStyle(color: Color(0xff323232), fontSize: 13, )),
Text(message.content, style: TextStyle(color: Color(0xff323232), fontSize: 13)), )
],
),
], ],
// mainAxisSize: ,
// children: <Widget>[
// ],
// padding: EdgeInsets.only(right: ),
// mainAxisAlignment: MainAxisAlignment.start,
); );
...@@ -85,8 +92,10 @@ class MessageItem extends StatelessWidget { ...@@ -85,8 +92,10 @@ class MessageItem extends StatelessWidget {
child: new Padding( child: new Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: new Column( child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
titleRow, titleRow,
// Text(message.content, maxLines: 2, textAlign: TextAlign.left, overflow: TextOverflow.ellipsis, style: TextStyle(color: Color(0xff323232), fontSize: 13, )),
contenRow, contenRow,
timeRow, timeRow,
], ],
...@@ -107,7 +116,5 @@ class MessageItem extends StatelessWidget { ...@@ -107,7 +116,5 @@ class MessageItem extends StatelessWidget {
), ),
), ),
); );
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment