使用缓存框架后response was not ok
来源:7-4 列表实现&数据渲染【封装与复用】
慕少6035530
2020-12-20
我按照您的代码一步步往下写,缓存框架测试可用后,popular模块完成缓存框架的调用,然后调试报告response was not ok,我也不会调试,所以看不出原因来,您看哪里有问题,
我已经把问题锁定在了popular页面
const URL = 'https://api.github.com/search/repositories?q=';
const QUERY_STR = '&sort=stars';
const THEME_COLOR = '#888';
export default class PopularPage extends Component {
constructor(props) {
super(props);
this.tabNames = ['C', 'C++', 'Java', 'C#', 'JavaScript', 'ECMAScript', 'React', 'React Native', 'VUE', 'Angular'];
};
genTabs() {
const tabs = {};
this.tabNames.forEach((items, index) => {
tabs[`tab${index}`] = {
screen: props => <PopularTabPage {...props} tabLabel={items} />,
navigationOptions: {
title: items, }
};
});
return tabs;
};
render() {
const { navigation } = this.props;
const TopTabNavigator = createAppContainer(
createMaterialTopTabNavigator(
this.genTabs(), {
tabBarOptions: {
tabStyle: styles.tabStyle,
upperCaseLabel: false,
scrollEnabled: true,
style: {
backgroundColor: '#028'
},
indicatorStyle: styles.indicator,
labelStyle: styles.labelStyle,
}
}
)
);
return (
<View style={styles.container}>
{/* <Text style={styles.titleText}>Popular</Text> */}
<TopTabNavigator />
</View>
);
}
};
class PopularTab extends Component {
constructor(props) {
super(props);
const { tabLabel } = this.props;
this.storeName = tabLabel;
};
componentDidMount() {
this.loadData()
};
loadData() {
//https://api.github.com/search/repositories?q=java
const { onLoadPopularData } = this.props;
const url = this.genFetchUrl(this.storeName);
onLoadPopularData(this.storeName, url)
};
genFetchUrl(KEY) {
return URL + KEY + QUERY_STR;
};
renderItem(data) {
const item = data.item;
return <View style={{ marginBottom: 10 }}>
<Text style={{ fontSize: 14, backgroundColor: '#f80' }}>{JSON.stringify(item)}</Text>
</View>
}
render() {
const { popular } = this.props;
let store = popular[this.storeName];
if (!store) {
store = {
items: [],
isLoading: false
}
};
return (
<View style={styles.container}>
<FlatList
data={store.items}
renderItem={data => this.renderItem(data)}
keyExtractor={item => item.id}
refreshControl={
<RefreshControl
title={'Loading'}
titleColor={THEME_COLOR}
colors={[THEME_COLOR]}
refreshing={store.isLoading}
onRefresh={() => this.loadData()}
tintColor={THEME_COLOR}
/>
}
/>
</View>
)
}
};
const mapStateToProps = state => ({
popular: state.popular
});
const mapDispatchToProps = dispatch => ({
onLoadPopularData: (storeName, url) => dispatch(action.onLoadPopularData(storeName, url)),
});
const PopularTabPage = connect(mapStateToProps, mapDispatchToProps)(PopularTab);
1回答
-
网络请求问题还需要调试下来定位具体问题,调试在2-3有讲解;
1.看下有没有设置代理如果有将代理关闭;
2.github的接口有请求次数限制,如果一会好一会不好那就是被github限流了;
3.调试时在输出is not ok的地方打个debugger,然后按照2-3的讲解开启调试,看下网络的具体返回;052020-12-23
相似问题