0

I am trying to fetch data from 000webhost server into my xamarin.android application. connection of php mysqldatabase is working good but I am getting JSONException in one of my classes shown below.

DataPhraser.cs

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

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Org.Json;
using Object = Java.Lang.Object;
using String = System.String;

namespace database_test.database.mySQL
{
    class DataPhraser : AsyncTask
    {
        Context c;
        private Spinner sp;
        private String jsonData;
        JavaList<string> Universities = new JavaList<string>();
        private ProgressDialog pd;

        public DataPhraser(Context c, Spinner sp, string jsonData)
        {
            this.c = c;
            this.sp = sp;
            this.jsonData = jsonData;
        }


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

            pd = new ProgressDialog(c);
            pd.SetTitle("Parse Data");
            pd.SetMessage("Parsing Data..... Please Wait");
            pd.Show();
        }

        protected override Object DoInBackground(params Object[] @params)
        {
            //throw new NotImplementedException();
            return this.ParseData();
        }

        protected override void OnPostExecute(Object result)
        {
            base.OnPostExecute(result);

            pd.Dismiss();

            if (Integer.ParseInt(result.ToString()) == 0)
            {
                Toast.MakeText(c, "unable to Prase", ToastLength.Short).Show();
            }
            else
            {
                ArrayAdapter<string> adapter = new ArrayAdapter<string>(c, Android.Resource.Layout.SimpleListItem1, Universities);
                sp.Adapter = adapter;

                sp.ItemSelected += sp_ItemSelected;
            }

        }

        private void sp_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Toast.MakeText(c, Universities[e.Position], ToastLength.Short).Show();
        }

         private int ParseData()
        {

            try
            {
                JSONArray ja = new JSONArray(jsonData);
                JSONObject jo = null;

                Universities.Clear();

                for (int i = 0; i < ja.Length(); i++)
                {
                    jo = ja.GetJSONObject(i);

                    String name = jo.GetString("Country");

                    Universities.Add(name);

                }

                return 1;
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e);
            }

            return 0;

        }
    }
}

I am getting error at " JSONArray ja = new JSONArray(jsonData)" this point of the code.

Mysqldatabase is

enter image description here

1
  • What is the value of jsonData when you get the exception? Commented Oct 18, 2019 at 19:59

1 Answer 1

1

According to your gson, you can try to use Newtonsoft.Json Nuget ,for example:

namespace QuickType
{
using System;
using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class Welcome
{
    [JsonProperty("Options")]
    public Option[] Options { get; set; }
}

public partial class Option
{
    [JsonProperty("ID")]
    public long Id { get; set; }

    [JsonProperty("University Name")]
    public string UniversityName { get; set; }

    [JsonProperty("Country")]
    public string Country { get; set; }

    [JsonProperty("Course")]
    public string Course { get; set; }

    [JsonProperty("Field of Study")]
    public string FieldOfStudy { get; set; }

    [JsonProperty("Course Language")]
    public string CourseLanguage { get; set; }

    [JsonProperty("Type of Institution")]
    public string TypeOfInstitution { get; set; }
}

public partial class Welcome
{
    public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}

internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters =
        {
            new IsoDateTimeConverter { DateTimeStyles = 
             DateTimeStyles.AssumeUniversal }
        },
    };
 }
}

For more details, you can check: https://app.quicktype.io/#l=cs&r=json2csharp

Note: you can just copy your json string to the left part of above link, then it will convert the json into relative data models.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.