Why do i get this Json exceptcion at parsing?

ErellontErellont Member ✭✭

Hello there;

First than anything, my apologies for my English.

Well, i just started a few days ago with a new adventure, trying to make some apps for smartphones devices. So i pushed up a dev db and a php web service with just only a get request. When i get in my local network server "protocol://myip/webservicern/listado.php/" i get back this following JSON:

[{"codigo":"1","nombre":"Ivan","edad":"19","correo":"[email protected]","pass":"123"}]

So, what i trying to do is from an android device do a http get request to parse this json in my app, for the moment just in a label.

So thats my class called User where is my "data structure"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RN2.Clases
{
    public class User
    {
        public int codigo { get; set; }
        public string nombre { get; set; }
        public int edad { get; set; }
        public string correo { get; set; }
        public string pass { get; set; }

    }
}

Also this is my MainPage.cs

namespace RN2
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

        }

        private void Btn1_Clicked(object sender, EventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                Result client = new Result();
                var res = await client.Get<User>(myurlgoeshere); //cant publish the question if i keep the url 
                if (res != null)
                {
                    LabelChange.Text = "se cambio!!";

                }
            });

        }
    }
}

There suppose that when i touch the button, i call this method client.Get(myurlgoeshere).

Cause it wasnt working i commented the following class to see why it wasnt doing what it pretend.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;

namespace RN2.Clases
{
 public class Result
    {
        public async Task<T> Get<T>(string url)
        {


                HttpClient client = new HttpClient();
                var response = await client.GetAsync(url);
                //if (response.StatusCode== System.Net.HttpStatusCode.OK)
                // {
                    var jsonstring = await response.Content.ReadAsStringAsync();
                    return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonstring);
              //  }

             // return default(T);
        }
    }
}

Finally this is my excepction: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

Somewhere i read something about "Skip linking assamblies" under android option. It didnt work btw.

I tested this on virtual Android device and my own android device neither worked.

This is my first time posting something i hope that everything is clear.

By the way thank you

Answers

  • ErellontErellont Member ✭✭

    Update 1: With Postman app, i verifyed that the json is in a correct format:

    [
        {
            "codigo": "1",
            "nombre": "Ivan",
            "edad": "19",
            "correo": "[email protected]",
            "pass": "123"
        }
    ]
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Are you sure that is the actual JSON being received? I may be misreading it but it looks like the bad character is '<', which looks suspiciously like your service is sending XML.
  • ErellontErellont Member ✭✭

    Hi James, thanks for your reply. I'm pasting my php file that runs in my WampServer

    <?php
    
    
        $cnx=new PDO("mysql:host=127.0.0.1;port=3308;dbname=rionegro","root","");
    
        $res=$cnx->query("select * from usuarios");
    
    
        $datos=array();
    
    
        foreach($res as $row )
        {
            array_push($datos,array(
                'codigo'=>$row['codigo'],
                'nombre'=>$row['nombre'],
                'edad'=>$row['edad'],
                'correo'=>$row['correo'],
                'pass'=>$row['pass'],
            ));
        }
    
        echo utf8_encode(json_encode($datos));
    
    ?>
    

    I use json_encode of datos which is the array holding each json

Sign In or Register to comment.