Tuesday, April 23, 2013

Android PUSH: GCM error: Missing registration_ids field

Hello,                                                                        

When trying to send the registration_id in the format requested in the site I got the error: 'Missing registration_ids field'. Sending it as an array worked as shown below.

Initial: 
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2...

{
  "registration_id" : "APA91bHun4MxP5egoKMwt2KZF....",
  "data" : {
    "Team" : "Portugal",
    "Score" : "3",
    "Player" : "Varela",
  },
}
Gives the following error: Missing "registration_ids" field

Use:                                                                            {
  "registration_ids" : ["APA91bGSnrAvxspEngNfGqd4JDsl00..."],
  "data" : {
    "Team" : "Portugal",
    "Score" : "3",
    "Player" : "Varela",
  },
}

Hope this helps !

Sunday, April 21, 2013

Support Apostrophes in Android Application names \ strings.xml file

Hello,

If you want to support apostrophes in your Android application name (or another field in your strings.xml file) and have got an error, take a look at the information below.

Application names do not support the apostrophe by default. An application name in the strings.xml file (res > values > strings.xml) in the format:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Who's there</string>

Will give you an error.

If you want to support this you can try escaping the apostrophe:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Who\'s there</string>
(this hint is already there in Eclipse if you hover the mouse over the red cross in the strings.xml file)

else you can enclose the full name in double quotes:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">"Who's there"</string>



Hope this helps and makes your life easier ! :)




XML manipulation in C# - reading from two nodes, concatenating them and adding the value as a new node.

Hello,

I am a C# beginner and I had the requirement of reading two nodes from an XML file concatenating them and inserting the concatenated value in a new node.

The code I used is given below, and hopefully will help you to get your task done (which should not be an evil purpose :) ). The sample XML used can be found below the code.

What is being done:
- Read the values from a node and add them to a list (using XmlNodeList and List<string>).
- Do the same for the other node from which I need the values.
- Concat the values of the two lists above.
- Add the concatenated values as a new node to the XML (using XmlDocument 's CreateNode method).

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
//use XmlDocument to access the XML
            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("C:\\Documents and Settings\\learner\\Desktop\\Daily\\XML\\A.xml");
            xmlDoc.LoadXml(response);

//Get the node list into a XmlNodeList object using XmlDocument 's SelectNodes method
            XmlNodeList CustomerNumList = xmlDoc.SelectNodes("//CustomerChosen/Customer/CustomerID/CustomerNo/text()");

            List<string> numberVals = new List<string>();

//Iterate the XmlNodeList
            for (int i = 0; i < CustomerNumList.Count; i++)
            {
                numberVals.Add(CustomerNumList[i].Value);
            }

            List<string> functionCodeVals = new List<string>();

//same as above - but a different node
            XmlNodeList CustomerFunctionList = xmlDoc.SelectNodes("//CustomerChosen/Customer/CustomerFunction/Code/text()");
            for (int i = 0; i < CustomerFunctionList.Count; i++)
            {
                functionCodeVals.Add(CustomerFunctionList[i].Value);
            }

            List<string> concatNumCodes = new List<string>();

//Concatenating here
            for (int i = 0; i < numberVals.Count; i++)
                concatNumCodes.Add(numberVals[i] + "-" + functionCodeVals[i]);

            string concatNumCodeString = String.Join(",", concatNumCodes);
           

            XmlNodeList CustomerSelectionList = xmlDoc.SelectNodes("//CustomerSelection/Customer");
            for (int i = 0; i < CustomerSelectionList.Count; i++)
            {
//making a new node here using XmlDocument's CreateNode method
                XmlNode nodeDeterminedCustomers = xmlDoc.CreateNode("element", "DeterminedCustomers", "");
                nodeDeterminedCustomers.InnerText = concatNumCodeString;
                CustomerSelectionList[i].AppendChild(nodeDeterminedCustomers);

            }

            Console.WriteLine(concatNumCodeString);
            xmlDoc.Save(Console.Out);
            Console.ReadLine();
// sharing to make your life easy, hope this helps ! good luck :)

        }

    }

}



XML:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP:Body>
      <ns0:CustomerDetermByElemResp xmlns:ns0="http://learner.com/CRM/BusinessCustomer">
        <CustomerChosen>
            <Customer>
               <CustomerID>
                  <CustomerNo>6321565426</CustomerNo>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000003</Code>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <DeterminationTypeCode>2</DeterminationTypeCode>
            </Customer>
            <Customer>
               <CustomerID>
                  <CustomerNo>5236214589</CustomerNo>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000004</Code>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <DeterminationTypeCode>2</DeterminationTypeCode>
            </Customer>
         </CustomerChosen>
         <CustomerSelection>
            <Customer>
               <CustomerID>
                  <CustomerNo>953216475</CustomerNo>
                  <CustomerGUID>DE435FD45T</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000015</Code>
                  <Text>Contact Person</Text>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <CustomerDescription>0430 Peter</CustomerDescription>
               <CustomerAddress>1265 SriLanka</CustomerAddress>
               <DeterminationTypeCode>1</DeterminationTypeCode>
            </Customer>
            <Customer>
               <CustomerID>
                  <CustomerNo>2153654892</CustomerNo>
                  <CustomerGUID>E234R32ED4</CustomerGUID>
               </CustomerID>
               <DocumentID>00000000000000000000000000000000</DocumentID>
               <CustomerFunction>
                  <Code>00000123</Code>
                  <Text>Contact Person</Text>
               </CustomerFunction>
               <IsMainCustomer>true</IsMainCustomer>
               <RelatedCustomerID>
                  <CustomerGUID>00000000000000000000000000000000</CustomerGUID>
               </RelatedCustomerID>
               <CustomerDescription>0124 John</CustomerDescription>
               <CustomerAddress>1234 Srilanka</CustomerAddress>
               <DeterminationTypeCode>1</DeterminationTypeCode>
            </Customer>
         </CustomerSelection>
         <CustomerList/>
      </ns0:CustomerDetermByElemResp>
   </SOAP:Body>
</SOAP:Envelope>

Friday, March 29, 2013

Android webview cookies and form data

I was doing some research on clearing cookies on the Android webview (version 2 and version 4) and noticed that clearing the cookies alone was not enough to get rid of the form data. Given below are some findings so that anyone looking can see the results without repeating the experimentation...

CookieManager.getInstance().removeAllCookie();

Android version 2

Before calling - CookieManager.getInstance().removeAllCookie()

Cookies table – webview.db


Cache table – webviewCache.db


After calling  - CookieManager.getInstance().removeAllCookie()

Cookies table – webview.db





Cache table – webviewCache.db





As seen above the cookies table data are deleted but not the cache table data.


Android version 4

Before calling - CookieManager.getInstance().removeAllCookie()

Cookies table – webview.db

This is no longer used to store data.

Cookies table – webviewCookiesChromuim.db


There is another DB called webviewCookiesChromuimPrivate.db which I did not see data being populated to (maybe because I was not using private browsing as seen below).

A quote from another website:
"what is peculiar is that the “webview.db” file still contains the legacy “cookies” table, however in testing this was never populated. Instead, a new database named “webviewCookiesChromium.db” is used to store cookie data.

“webviewCookiesChromiumPrivate.db”. This database contains cookies transmitted while an “Incognito Tab” (the private browsing feature) is being used. The structure is identical to the other database; however, when the incognito tab is closed the file is truncated to 0 bytes.* " (http://digitalinvestigation.wordpress.com/2011/12/02/android-ice-cream-sandwich-browser-cookies-and-other-artefacts/)


After calling  - CookieManager.getInstance().removeAllCookie()

Cookies table – webview.db

Well there was no data to begin with there so lets move on...


Cookies table – webviewCookiesChromuim.db



The cookies table data was deleted.

However if we log in or save a form, that data is not cleared by simple clearing the cookies:
as seen below, those are in different tables:



So basically I think it is a good practice to delete the form data as well when deleting the cookies. code snippet to do that: 

  WebViewDatabase webViewDatabase =WebViewDatabase.getInstance(context);
        webViewDatabase.clearHttpAuthUsernamePassword();
        webViewDatabase.clearUsernamePassword();
        webViewDatabase.clearFormData();

This code removes the data shown in the screenshot above.
There is also a method which can be used to clear the cache, even though it is a webview instance method it removes the cache for all the webviews in your application. More information at: http://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean) 

Hope this helps. This was done using DDMS to pull the DB and then opening them in SQLlite manager of FireFox. 

- May all beings be well and happy.