TheQ
TheQ

Reputation: 2009

how to add a custom object into azure in android?

I have this CardModel class and i cannot add a cardmodel object into azure database. I have a cardmodel table, but nothing gets added, but my app builds fine and doesn't crash.

The only custom object i have in here is HoursOpen, everything else is a double, string, int or an arraylist of the previous.

Here is my cardmodel class.

public class CardModel
{

    private double latitude;
    private double longitude;
    private int cardImage;
    private String cardName;
    private String cardLocality;
    private String caption;
    private ArrayList<String> tags = new ArrayList<>();
    private ArrayList<String> images = new ArrayList<>();
    private int cardID;
    public  int defaultPoints = 100;
    private String user_id;
    private String website = "www.facebook.com";
    private String phoneNumber = "4086671474";
    private HoursOpen hoursOpen = new HoursOpen();
    private String referenceToImages;
    private String id;


    public CardModel()
    {
        setInitialID();
    }

    //Getters, if you want to receive information from this class.
    public int getCardID()
    {
        return cardID;
    }

    public String getReferenceToImages()
    {
        return referenceToImages;
    }

    public String getCardName()
    {
        return cardName;
    }

    public String getCardLocality()
    {
        return cardLocality;
    }

    public int getCardImage()
    {
        return cardImage;
    }

    public int getDefaultPoints()
    {
        return defaultPoints;
    }

    public String getUser_id()
    {
        return user_id;
    }

    public String getCaption()
    {
        return caption;
    }

    public ArrayList<String> getImages()
    {
        return images;
    }

    public double getLatitude()
    {
        return latitude;
    }

    public double getLongitude()
    {
        return longitude;
    }

    public String getWebsite()
    {
        return website;
    }
    public String getPhoneNumber()
    {
        return phoneNumber;
    }
    public HoursOpen getHoursOpen()
    {
        return hoursOpen;
    }
    public ArrayList<String> getTags()
    {
        return tags;
    }

    //Setters
    public void setCardImage(int image)
    {
        this.cardImage = image;
    }

    public void setReferenceToImages(String referenceToImages)
    {
        this.referenceToImages = referenceToImages;
    }

    public void setCardName(String cardName)
    {
        this.cardName = cardName;
    }

    public void setcardPoints(int points)
    {
        this.defaultPoints = points;
    }

    private void setInitialID()
    {
        Random randomNumberGenerator = new Random();
        cardID = randomNumberGenerator.nextInt(1000000);
    }

    public void setCardLocality(String locality)
    {
        this.cardLocality = locality;
    }

    public void setCaption(String caption)
    {
        this.caption = caption;
    }

    public void setImages(ArrayList<String> images)
    {
        this.images = images;
    }

    public void setLatitude(double latitude)
    {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude)
    {
        this.longitude = longitude;
    }
    public void setWebsite(String website)
    {
        this.website = website;
    }
    public void setPhoneNumber(String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }
    public void setHoursOpen(HoursOpen hoursOpen)
    {
        this.hoursOpen = hoursOpen;
    }
    public void setTags(ArrayList<String> tags)
    {
        this.tags = tags;
    }
    public void addTag(String tag)
    {
        tags.add(tag);
    }
    public void setUser_id(String user_id)
    {
        this.user_id = user_id;
    }
    public void setCardID(Integer cardID)
    {
        this.cardID = cardID;
    }
}

Now in my activity class i have the followning:

  1. //Azure private MobileServiceClient mClient;

  2. in onCreate()

    try {
        mClient = new MobileServiceClient("my link");
        CardModel testCard = new CardModel();
        testCard.setName("sampleCard");
        azureExample(testCard);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    
  3. private void azureExample(CardModel cardModel){ mClient.getTable(CardModel.class).insert(cardModel); }

Upvotes: 0

Views: 78

Answers (1)

Peter Pan
Peter Pan

Reputation: 24128

According to the offical document How to use the Android client library for Mobile Apps, there is the content related to your issue in the sub-section "How to: Customize serialization" as below.

The client assumes that the table names, column names, and data types on the backend all match exactly the data objects defined in the client. There can be any number of reasons why the server and client names might not match. In your scenario, you might want to do the following kinds of customizations:

  • The column names used in the App Service table don't match the names you are using in the client.
  • Use an App Service table that has a different name than the class it maps to in the client.
  • Turn on automatic property capitalization.
  • Add complex properties to an object.

The bold part above is the reason of your issue. Meanwhile, the solution has been include in the document, as below.

How to: Store an object or array property into a table

So far, our serialization examples have involved primitive types such as integers and strings. Primitive types easily serialize into JSON. If we want to add a complex object that doesn't automatically serialize to JSON, we need to provide the JSON serialization method. To see an example of how to provide custom JSON serialization, review the blog post Customizing serialization using the gson library in the Mobile Services Android client.

Upvotes: 1

Related Questions