My Api 返回的响应与下面定义的常量"设备"完全相同。如果我不调用 api,我会用单行数据正确填充列表视图,但如果我使用 Api 响应填充数据源,我会得到空的 160 行。
我的设置状态有什么问题?
const devices = [
{
"DeviceID": 4,
"DeviceCode": "ODPRC-HB-4",
"ContactName": "Sudhir",
"GPS_Lat_Long": "15.396863, 74.012382",
"DeviceName": "Sudhirs Device"
}
];
class Devices extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
});
this.state = {
dataSource: ds.cloneWithRows(devices)
}
}
componentDidMount() {
this.getDeviceList();
}
getDeviceList() {
let request = new Request('https://myapiWithQueryString.com');
request.addHeaders({ 'Content-Type': 'application/json'});
RNETWORK.get(request, () => {
}, (response) => {
if (response.getError()) {
alert(response.getError().message);
} else {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(response.getBodyString())
});
alert(response.getBodyString());
}
});
}
试试这个:
dataSource: this.state.dataSource.cloneWithRows(JSON.parse(response.getBodyString()))
注意:我假设getBodyString实际上返回了一些东西,因为我不记得响应接口中存在该方法。