Reputation: 894
I need to connect my project with JSON API
, I followed many documentation from flutter website and stack overflow issues but I got many problems, I wrote some codes to connect with JSON
but when I am running with Android SDK I got this error:
But after added this line in AndroidMainifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<uses-permission android:name="android.permission.INTERNET" />
I got the same error above!! I'm sure I connect with WiFi!
When I'm running with iOS SDK I got this error:
Code:
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
List<Category> _catgList =new List<Category>();
Future<List<Category> > fetchCategory() async {
final response = await http.get('url of json'); // Here first error
if (response.statusCode == 200) {
List<dynamic> values = new List<dynamic>();
values = json.decode(response.body); // Here second error
if(values.length>0){
for(int i=0;i<values.length;i++){
if(values[i]!=null){
Map<String,dynamic> map=values[i];
_catgList.add(Category.fromJson(map));
debugPrint('Id-------${map['id']}');
debugPrint('Id-------${map['title']}');
}
}
}
return _catgList;
} else {
throw Exception('Failed to load post');
}
}
class Category {
final int id;
String title;
Category({this.id, this.title});
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
id: json['id'],
title: json['title'].toString(),
);
}
}
JSON:
{
"status" : "ok",
"count" : 14,
"categories" : [
{
"id" : 2,
"title" : "world"
},
{
"id" : 13,
"title" : "games"
},
{
"id" : 54,
"title" : "woman"
},
{
"id" : 50,
"title" : "health"
},
{
"id" : 48,
"title" : "kitchen"
},
{
"id" : 4292,
"title" : "applications"
},
{
"post_count" : 51,
"id" : 51,
"title" : "culture"
},
{
"id" : 49,
"title" : "sport"
},
{
"id" : 8,
"title" : "travel"
},
{
"id" : 53,
"title" : "policy"
},
{
"id" : 52,
"title" : "cars"
},
{
"id" : 20,
"title" : "tech"
},
{
"id" : 4293,
"title" : "gharaeb"
},
{
"id" : 18,
"title" : "business"
}
]
}
What's wrong in my code??
Upvotes: 0
Views: 563
Reputation: 316
You missed this for json.decode or encode.
import 'dart:convert'
And please refer my code for http request.
try{
var client = new http.Client();
var url = "Your Url";
var response = await client.get(url);
if(response.statusCode == 200){
var results = json.decode(response.body);
if(results['status'] == 'ok'){
//if your result have loop
for(var item in results['categories']){
//enter your code
print(item['id'] + ":" + item['title']);
}
//you can use this your response value
print(results['your_key1']['your_key2']);
}
}
}on Exception catch(err){
print("Error: $err");
}
I hope to help you.
Upvotes: 1
Reputation: 3892
First error is probably due to some network issues or setup issue. For the second problem, replace this line in your code
values = json.decode(response.body);
to
values = json.decode(response.body)["categories"];
As is obvious from the JSON data your provided, the json.decode
method returns a map and you can get your list of categories by json.decode(response.body)["categories"]
.
Upvotes: 1