Posted on 1 Comment

Consume php backend nested array on a C# Application – Json.NET

Lets assume you got a MYSQL database that contains data for your application and you want to consume this information on a C# Application in order to handle it on a client.

For the sake of this example the following table will be parsed

As depicted the following information is returned from the api:

  • the state of the response (succeeded or not)
  • a message that will show the response result
  • the actual result that in this case is a list of trips

Each trip contains id, fromlocation, tolocation, date and time properties that have to be returned to the application.

The php code that was used to create the data from the database is the following:

         $trips =array();
         while ( $row = $stmt->fetch() ) {         
         $obj["id"]= $row['id'];
         $obj['fromlocation']= $row['fromlocation'];
         $obj['tolocation'] = $row['tolocation'];       
         $obj['date']= $row['date'];
         $obj['time'] = $row['time'];         
         array_push($trips,$obj);
         }

         $response["success"] = 1;
         $response["message"] = "Trips returned successfully!";
         $response["trips"] = $trips;
         die(json_encode($response));

So in order to parse this json result we will need a plugin like Newtonsoft.Json

The class that will be used to encapsulate the response would be the following:

public class GetTrips
    {
        [JsonProperty("success")]
        public int Success { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }
        [JsonProperty("trips")]
        public Trip[]  Trips { get; set; }

       
        public class Trip
        {
            [JsonProperty("id")]
            public string Id { get; set; }
            [JsonProperty("fromlocation")]
            public string FromLocation { get; set; }
            [JsonProperty("tolocation")]
            public string ToLocation { get; set; }
            [JsonProperty("date")]
            public string Date { get; set; }
            [JsonProperty("time")]
            public string Time { get; set; }
        }

    }

And then we will consume the json response with the following code in C#. In this example data from the API came from a POST request on a web Form.

public async TaskGetTripsAsync(string username,string password)
        {
            try
            {
                var keyValues = new Liststring, string>>
                {
                    new KeyValuePair("username",username),
                    new KeyValuePair("password",password)
                };

                var request = new HttpRequestMessage(HttpMethod.Post, Url);
                request.Content = new FormUrlEncodedContent(keyValues);

                var client = new HttpClient();
                var result = await client.SendAsync(request);
                var content = await result.Content.ReadAsStringAsync();
                var tripsResult = JsonConvert.DeserializeObject (content);
                return tripsResult;
            }
            catch (Exception)
            {
                return null;
            }
        }

Posted on 2 Comments

Install Entrust ssl chain certificate apache 2 – Red Hat Configuration

In order to install successfully a chain certificate (in my environment it was issued by Entrust) on a Red hat you should edit the following entries in the /etc/httpd/conf.d/ssl.conf apache file.

#Server Certificate 
SSLCertificateFile /path/to/public.pem

#Server Private Key
SSLCertificateKeyFile /path/to/private.pem

#Server Certificate Chain
SSLCertificateChainFile /path/to/chain.crt

#Certificate Authority
SSLCertificateFile /path/to/root-ca.pem

After the changes you should restart apache so that the changes take place. Keep in mind that if the certificate is password protected, you will be asked for an ssl passphrase that you should provide in order for the restart to be completed.

service httpd restart

You can ensure that the certificate is installed successfully by using the following command:

openssl s_client -connect HOSTNAME:443

Your response should look like:

Verify return code: 0 (ok)

 

 

Posted on Leave a comment

ViewModel Image binding based on Condition – XAML

If you want to show a specific image in a Xamarin.Forms app based on a condition, you can Image.Triggers

For example you have an Image element in your XAML and you want to show a different image based on a database query. In order to do that, you should place the pictures that you want to use in the appropriate folders.

  • For iOS the pictures must be located under Resources folder.
  • For Android the pictures should be in Resources/drawable
  • For UWP you must place the pictures in the root directory of the project.
 <Image WidthRequest="80"  Source="{Binding Category}" HeightRequest="80">
                                <Image.Triggers>
                                    <DataTrigger TargetType="Image" Binding="{Binding Category}" Value="ID Number">
                                        <Setter Property="Source" Value="id.png" />
                                    </DataTrigger>
                                    
                                     <DataTrigger TargetType="Image" Binding="{Binding Category}" Value="Tax Identification Number">              
                                        <Setter Property="Source" Value="tax.png" />
                                    </DataTrigger>

                                     <DataTrigger TargetType="Image" Binding="{Binding Category}" Value="Other">              
                                        <Setter Property="Source" Value="other.png" />
                                    </DataTrigger>
                                    
                                </Image.Triggers>
    </Image>

As a result your image will change upon the value of the Category element. If the Category value would be ID Number then the image should be id.png etc..

Posted on Leave a comment

Map Windows OS disks with VMware hard disks

It is a bit tricky to map Windows OS disks with VMware disks especially when two or more disks have the same size, because vmware does not provide the disk name, but only scsi identifiers. As a result if I had to extend a particular disk, I must find the right one.

From VMware hard disk Advanced Settings, you can find a SCSI code for a particular disk 1:2 that vmware names hard disk 2. Note that hard disk 2 could ne different from Hard disk 2 that the Windows OS would name. You could determine that from the capacity of the Disk if they were different, but it could be also different even when the capacity (GB) were the same.

 

So how could you do the mapping?

First of all you should run the following powershell command. It would give you scsi identifiers for all Windows OS disks.

get-wmiobject -class Win32_DiskDrive | select deviceID,size,scsiport,scsitargetid

 

Windows OS start numbering controllers from a random positive number and adds one for each subsequent controllers. As a result if the numbering starts from 3, then 3 scsiport would match to 0 scsiport in VMware (because numbering starts from 0). In the particular example if you would see 4:2 from the powershell command, it would be the disk that you can see in the picture above.