Issue
I'm new in mobile apps and now developing an app with xamarin forms. There is a website which i developed with django (sqlite3 db), and now i'am trying to consume data from it and display in my mobile app in listvew. Any thoughts how to achieve it. I've tried this but it doesn't work. Should i use rest api?
public class LandingViewModel : INotifyPropertyChanged
{
private List<Dishes> _menuList { set; get; }
public List<Dishes> MenuList
{
get
{
return _menuList;
}
set
{
if(value != _menuList)
{
_menuList = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public LandingViewModel()
{
GetDataAsync();
}
private async void GetDataAsync()
{
HttpClient client = new HttpClient();
var response = await client.GetAsync("https://mysite.ru/project/");
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var menu = JsonConvert.DeserializeObject<List<Dishes>>(content);
MenuList = new List<Dishes>(menu);
}
}
models:
public class Dishes
{
public int id { get; set; }
public string description { get; set; }
public string image { get; set; }
public DateTime published { get; set; }
}
my database in django:
operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('description', models.TextField(blank=True)),
('image', models.ImageField(blank=True, upload_to='pictures/')),
('published', models.DateTimeField(verbose_name='publishing date')),
],
),
]
Solution
i solved the problem
in postgresql database allowed remote connection: in postgresql.conf replaced line listen_addresses = 'localhost' with listen_addresses = '*'
allowed tcp\ip connection on port 5432
in my web api established connection width db
NpgsqlConnection connection; public string _name; public string _description; public string _image; private readonly MenuContext _context; public MenuController(MenuContext context) { _context = context; string connectionString = "Server=server; Port=5432; User Id=user; Password=password; Database=database"; try { connection = new NpgsqlConnection(connectionString); connection.Open(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } NpgsqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM table"; try { NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { _name = reader[1].ToString(); _image = reader[2].ToString(); _description = reader[3].ToString(); _context.Menus.Add(new Menu { name = _name, description = _description, image = "https://mysite.ru/media/" + _image }); _context.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } // GET: api/Menu [HttpGet] public async Task<ActionResult<IEnumerable<Menu>>> GetMenus() { return await _context.Menus.ToListAsync(); }
code in my app:
private async void GetDataAsync()
{
HttpClient httpClient = new HttpClient();
var content = await httpClient.GetStringAsync("https://locahost/api/Menu");
var menu = JsonConvert.DeserializeObject<List<Dishes>>(content);
MenuList = new ObservableCollection<Dishes>(menu);
}
and then displayed it in listview
Answered By - ilmir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.