Monday, 4 March 2019

Android: Connecting to Webserver for simple data

Dear Readers,

When we are working with app development, most of the time we require accessing the particular website for the data. The website may be generally ours or some other website that provides information like weather forecast sites etc.

Traditionally data from the site could be accessed using HTML page, and whenever new data is required we were required to reload the page or load all HTML file. This works fine for static data and when the site is not required to be dynamic. However, today websites needn't need to reload the page for loading extra content, for example, facebook, twitter, youtube and soon. Thanks to the work of many developers that provided us techniques to just get the content required in the existing webpage.
Many developers like to call this as API. API may sound big enough and fancy but it just means application program interface. It is not a programming language instead it is the technique by which we can load the data in the existing webpage without reloading page.

To, my readers who have come to read this post, I assume that you have some experience in web development. If not, don't worry, I will guide you on how to get the web development knowledge required for implementing API in android. Again, Don't get overwhelmed by name API, its just nothing then a technique for accessing content in the web so that we can update the page without reloading it.

Now, let us start on the topic:

FOR THOSE FAMILIAR WITH WEBSERVER:
--
Developers at android have been helpful enough to provide the library:
https://developer.android.com/training/volley
If you like to explore more by yourself please feel free to jump in the site to get started.
But, if you would like to go with me, Feel free to join me in remaining text,

Volley library is the library distributed by the developer at android.com.It is also available in GitHub. It is made with the intention to make the call to our web server easily to retrieve the data from the web server.

First, we need to add the library to our android module: app gradle file add:
compile 'com.android.volley:volley:1.1.1' inside dependencies and it should look like below.
dependencies {
    ...
    compile 'com.android.volley:volley:1.1.1'
}

Congratulation, you have added the volley library, now the thing will get easier,

Secondly, go to the activity or class from where you will be requesting the data to the server:

Oh yes, since you are working with the internet, don't forget to add the permission for internet in your manifest file as
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

It should be kept after the end of <application/> tag and it may look like this:
     </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

Now, you are all set to resume the work,
On the activity:
//RequestQueue is the class that represents the medium to queue many requests to the web server.
here I am validating users after they press login, so I have named validate_user for RequestQueue.

RequestQueue myRequestQueue = Volley.newRequestQueue(context);
String extraString="";
// IF the webserver accept the data by post request then extraString is not required.
String url="your_website_name.com/login.php";

//IF the webserver used the GET request to receive data 
//then,extraString="?varaible1=value1&variable2=value2";
//in my case for user authentication variable1 is user and variable2 is pass
extraString="?user=value1&pass=value2";
url=url+extraString;
Now, as we have formated the desired url in the format that our webbrowser accepts; we can now request the server for the data;
Thirdly, you have to set up the response listener which stores the values retured by the webserver; Also the error listener that executes when error occurs while connnecting to server So,
//Response listener
Response.Listener listener=new Response.Listener<String>() { //Please note <String> is used
    @Override    public void onResponse(String response) { //Note: Instead 
//of Object String is used..
        Toast.makeText(context, "Congrats!! Connected Successfully to Server ", Toast.LENGTH_SHORT).show();
        Toast.makeText(context, response, Toast.LENGTH_SHORT).show(); 
//the response is the string data retured by server..
   // it may be normal echo "random strings"; by php code..
    }
};

//the error is returned in the object <VolleyError> object so notice how the object 
//is being used below
Response.ErrorListener errorListener=new Response.ErrorListener() {
    @Override    public void onErrorResponse(VolleyError error) {
        Toast.makeText(context, "Connection failed to Server ", Toast.LENGTH_SHORT).show();
        Toast.makeText(context,error.getMessage(), Toast.LENGTH_SHORT).show();
    }
};
//Since we are requesting the server for the string data: we use the class StringRequest
// used by Volley Library

StringRequest user_auth=new StringRequest(Request.Method.POST,url,listener,errorListener){
    //Since we have used the method POST to push the data to the server so, The variables 
//and values are kept in
   //MAP... As variable1 is user and Variable2 is pass
    @Override    protected Map<String, String> getParams() throws AuthFailureError {
       Map<String, String>  params = new HashMap<String, String>();
       params.put("user", username);//username is a String that I obtained from user input
       params.put("pass", password);//password is a String that I obtained from user input
       return params;
} };
myRequestQueue.add(user_auth);
You should now be able to get data from your webserver if it is configured properly
demo : login.php
<?php
 echo "Hi, I am the text sent from the php file in the webserver. I am the String and I can send other data as well in JSON and XML format";
?>
save the file as .php and keep in the same directry as index.html.. 
Be sure that you can run the php server in your system.. If you would like to know more
Search install apache and php server in windows or linux... and there are quite good video to guide you setting up the server. 
For you convinience practing this code: index.html
<HTML> <HEAD> <title>User Authentication</title> </HEAD> <BODY> <BODY STYLE="bgcolor: green; color: green;"</> <CENTER> <P> Please enter your login Crenditials. </P> <FORM action="login.php" method="post"> <TABLE> <tr><td>Username:</td><td><input type="text" name="user" value=""></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" value=""></td></tr> </TABLE> <input type= "submit" value="Login"> </FORM> <CENTER> </BODY> </HTML>
I think this post was helpful for you, If you have any questions or doubts or any suggestion please comment below.
FOR THOSE NOT FAMILIAR WITH WEBSERVER:
I suggest you to search installing the apache and php in the system. After that, knowledge of HTML and basic PHP is requred to follow the post, To make more easier the HTML and Basic PHP file required for following the post is included in the post above. 
https://www.znetlive.com/blog/how-to-install-apache-php-and-mysql-on-windows-10-machine/
to learn more search install Apache and PHP in the window or linux

Monday, 8 August 2016

Black Hole Punctures the 4th Dimension

In this paper I will explain my understanding about the black hole and black hole leading to other dimension through the 4th dimension. For a current discussion I will be discussing only after the formation of a body that have properties to become a black hole. I would like to call this body as NewBaby for convenience in further discussion. This body i.e. the NewBaby possess high mass and pulls all the bodies nearby it into itself.

Assumption
#1 The NewBaby is resting on the Fabric of the Space.
#2 The Fabric of Space is elastic in nature and is elongated directly proportional to NewBaby mass.

Theory: 

1. The NewBaby will start to attract all the masses nearby under it's influence. In this process it's mass gets build up.

2. The built up in mass will lead to the growth of NewBaby to the "Kid". 
(Kid is a name given for NewBaby after significant mass built up for the convenience in discussion).

3. The Kid has much mass than the NewBaby so it stretches the Fabric of the Space more i.e. the more deeper funnel type well in Space Fabric then the depth of well formed by the NewBaby. 

4. The high mass of the Kid result in a deeper i.e more elongated Space Fabric by assumption #2

5. Given that, there are other masses near the well influence, this will further result more mass accumulation and the growth of Kid to the Adult.
(Adult is a name given for KID after significant mass built up for the convenience in discussion).

6. The Adult has the extreme mass which is near to Ultimate mass carrying capacity of the Space Fabric at that particular volume of Space Fabric where the Adult is located. i.e. Space Fabric is elongated near to Ultimate limit of Space Fabric elongation.

7. When the Adult's mass grows to the Ultimate mass capacity limit, the fabric gets punctured and  Adult enter the other dimension.

8. This other dimension could lead to other universe/s if they exists.


Further more:

1. The elastic Space Fabric bounce back if the puncture of the Space fabric takes place creating the strong impact in the elastic Space Fiber. It produces the disturbances that propagates through the Space Fabric affecting the other masses in the Space Fiber.
( I think these disturbances are recognized as Gravitational Waves by the Scientific community at this current time. I would personally like to congratulate LIGO for the success in detecting the Gravitational waves for the first time in Lab which is in fact the greatest achievement in mankind.)

2. I, to this point with no doubt believe the other dimension which I would like to call as 4th dimension is a Time Dimension, thus getting access to the 4th dimension which allows us to travel in time and of course to the Universe/s that are stacked up in the time dimension.

Possible Query

Ques.How the puncture in the Space Fabric leads to other dimension?
Ans. By the rule of Dimension Puncture: If a body of any dimension puncture its dimension in which it exists or enter in the hole in it's dimension it is definite to enter the other dimension if the other dimension exists through the a dimension higher than it's dimension.

Query Analogy:

Take a 3 or 4 sheet of  flexible paper, stretch it from all the all sides. Then use the pen and puncture the sheet from the top with the force and note that the tip of the pen travel to other similar dimension through through the height/depth dimension.

This exact analogy is applied to the 3D body. By the rule of Dimension Puncture : if the 3D body puncture its dimension or enters the hole in its dimension then it is definite to enter other dimension through the higher dimension which I would like to call as 4th dimension. 

Conclusion: 

I have published this paper to share my ideas and understanding about the black hole and understanding about this universe and beyond.
Furthermore this paper doesn't restrict the discussion about the black hole, It also defines the Dimension Puncture and give the insight about the way to the travel to other universe if  they are stacked in 4th dimension.
Additionally it  relates the disturbances created by impact after puncture in the Space Fabric to the Gravitational waves. 

About Me

I was born in Tulsipur -8, Dang, Nepal. I am fascinated about the things around me since the time I was conscious observer of this universe. When I was child I used to think about the effect of height and degree of impact. As I grew up I got answer to my question with the help of Science. This increased my further interest in Science mainly Physics.

"It is not the inability to see but it's the willingness to ignore that sets a invisible blind fold to the reality."

Umesh Gautam
ugautam007@gmail.com
(Aeronautical Engineer)

Thursday, 23 June 2016

Designing Android Interface : Themes

Android provides various built in themes that can be applied by adding the :theme attibute to Activity together with name of Activity or to Application label in the manifest file.  Adding the theme attribute to the Activity only changes the theme for an Activity where as Adding to the Application changes theme to all those activities except the activities that has :theme attribute.

You can choose to use your own theme or the them in android:style libraries. Choosing the theme is a very difficult as we have to apply the theme each time and see it by running the app.

Let us make it Simple and Effective
The normal themes are:
1. @android:style/Theme.Dialog is the theme name that make the activity appear as a dialog box.
2. @android:style/Theme,Translucent is also a theme name that makes you activity transparent. The background is visible.
3.@:style/yourTheme is the theme that you have create in the and place in style.

So what is a Style in Android:
Style defines the look and format of the view or the window. It is composed of collection of properties like
1. Height
2. Padding
3. Font Color
4. Font Size
and more
Style once defined can be linked to other views so that you should not write the properties already described in style for in each view in layout file.

Okay thats a Syle, the what is Theme ?
The answer is simple, the theme is a other name of the style if it is applied to the whole activity and all its views. Style is also the xml file with the views name like <TextView and its properties. So all the text views in Activity will have the properties of <TextView defined in the style and their additional properties like hint.
Below is the example that will show how to create style and Apply to the specific view in layout file or to the whole Activity(ie such style is called Theme)

#Defining Style: A style is a xml file that contains a view like<TextView> and its properties. It may contain only one view or collection of views and their properties.The xml file should be placed inside res/values, Example:

Right Click in the directory res/values the style xml file then chose the filename for xml, then you get,
<?xml version="1.0" encoding="utf-8"?>
<resources></resources>
inside <resources> </resources> place the <style></style> and provide the name and parent of the style and other properties as follows.
<?xml version="1.0" encoding="utf-8"?><resources>
    <style name="myStyleEditText" parent="@style/TextAppearance.AppCompat.Title">
          <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:textColor">@android:color/holo_blue_light</item>
        <item name="android:typeface">serif</item>
        <item name="android:layout_marginLeft">10dp</item>
    </style>
</resources>
Here the name is required/must and parent is the style from which our style "myStlyeEditText" inherit the properties i.e parent="@style/TextAppearance.AppCompat.Title"or it may be also be parent="@style/TextAppearance.AppCompat.Light" or any other you like.

That's all, a style is created with a name "myStlyeEditText" . To use this style we will do something like this in the view like <TextView> in the layout file.
<EditText    android:id="@+id/editText"   
 android:hint="@string/search"    
  android:layout_marginLeft="10dp"   
 android:layout_marginRight="10dp"    
style="@style/myStyleEditText"     />
Here in the View ie <EditText> style attribute is added which is reference to the style  "myStyleEditText" which we made. We can even include <item name="android:layout_marginLeft">10dp</item> in the style and remove the android:layout_marginLeft="10dp" from<EditText> and so on for other properties like hint,layout_marginRight.

Further more this style that we made now can also be used as a parent as

<style name="myStyleEditText.Red">
    <item name="android:textColor">@android:color/holo_red_dark</item>
</style>

This style is same as "myStyleEditText" but overrides the @android:color/holo_red_dark to Red color.

#Defining Theme:
To apply the theme for whole application include in<Application together with label as
android:theme="Theme.AppCompat.Light.DarkActionBar">
Or parent="Theme.AppCompat.Light.NoActionBar"
Also you may use the your theme which is defined like
<style name="myTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@android:color/holo_blue_light</item>
    <item name="android:colorBackground">@android:color/black</item>
</style>
And use it as android:theme="@style/myTheme"> in the manifest file. for <Application> or<Activity>

That's all, you have created your theme which is inherited from parent theme.

More at: https://developer.android.com/guide/topics/ui/themes.html#DefiningStyles

Wednesday, 22 June 2016

Android Handling Configuration Change to Update the Layouts for Different Orientation

There are generally two used methods to provide different layout for different orientation
First is to,

  1. Override the onConfigurationChanged() method and add setContentView(R.layout.layoutname) only if you have edited the manifest file and have added android:configChanges="orientation|screenSize" or android:configChanges="orientation" together with the android:name attribute of activity.
  2. If not then you can perform action to change layout in onCreate() method replacing setContentView(R.layout.layoutname);
  3. The technique is to find the current orientation (Portrait or Landscape) and then set the Content View with the Desired layout ,Example
    if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
        setContentView(R.layout.landscapeLayout);
    
    }
    else if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT) {
       setContentView(R.layout.portraitLayout);
    }
  4. Thus load the layout designed for Potrait or Lanscape by including the above code inside either onCreate() or in onConfigurationChanged() method.. IMPORTANT refer 1 and 2 to choose the method either onCreate() or onConfigurationChanged().
  5. IMPORTANT: Make sure that the layout xml file has different name for different orientations example R.layout.landscapeLayout and R.layout.portraitLayout layout names for Landscape and Portrait layout respectively

Second is to,

  1. Create a directory for Landscape and Portrait in the Layout folder inside res folder as
  2.  Create layout-land directory by right clicking on res folder and add paste the xml layout file for Landscape mode inside it, This file will be used in Landscape mode by android itself.
  3. The default path layout is for Portrait mode, and android use the file inside this for Portrait mode.
  4. IMPORTANT: Make sure that the layout xml file has same name in both directories layout-land and layout.
  5. That's all
More at:https://developer.android.com/training/basics/supporting-devices/screens.html

Android Handling Configuration Change

Configuration Changes may include one of these behaviors
  • Changes in Screen Orientation
  • Keyboard Availability Change
  • And the Change in Language
Normally the Configuration Changes results in the call of onDestroy() and onCreate() methods in a order.

To restore the state of the data of your application android call onSaveInstanceState() before onDestroy() and onRestoreInstanceState after onCreate(). It is simple, you can save the data in onSaveInstanceState() method and restore the same in onRestoreInstanceState() or onCreate() method.

 So, how do I handle my Configuration Changed?
 There are 2 methods normally used, First one is Simple method and Second is Compex. Simple is generally preferred if the data that need to be restored during configuration change is less where as Complex is preferred when required to restore large data as the use of simple methods will result lag in the USER Experience of USER INTERFACE.

Method : Simple Method
  • Above and on API 13 add following in Manifest file inside activity with activity Name 
    android:configChanges="orientation|screenSize"
  • Below API 13 add following in Manifest file inside activity with activity Name 
    
    android:configChanges="orientation"
  • And you are done. That's all.
  • If you need to perform some actions on configuration change then only Override the onConfigurationChanged() method of the Activity class inside your Class to perform additional action on Configuration Change but make sure you always call super.onConfigurationChanged();
  • Doing this, the Configuration change event will not call for onCreate() again and application data remains same.
Method : Complex Method
  • When the Configuration Changes and if the manifest file of activity doesn't include configchanges attribute unlike Simple Method above, then the android calls onSaveInstanceState in the event of Configuration Change unspecified in Manifect Activity.
  • Override the onSaveInstanceState() method and add the values to be saved. example
    protected void onSaveInstanceState(Bundle outState) {
         String myValue="SaveValue";
        outState.putStringArrayList("RECOVER", myValue);
         super.onsaveInstanceState();
    }
  • Then the android calls for onDestroy() method, on Create() and onRestoreInstanceState() in a order.
  • Override the onRestoreInstanceState() to recover the saved data in onSaveInstanceState() ,Example
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    
      String myValue;     
    myValue= savedInstanceState.getStringArrayList("RECOVER"); super.onRestoreInstanceState(savedInstanceState); }
  • This recovers the String named myValue having the content "SaveValue". The keyName which is RECOVER should be unique as it carry the String named myValue(a variable).
More at: https://developer.android.com/guide/topics/resources/runtime-changes.html

Thursday, 12 November 2015

Basis working of an Airplane;

Propeller
The engine provides the thrust that pushes the aircraft on the runway, After travelling certain distance the airflow on the wing surface is sufficient enough to create sufficient lift to counter weight and provides upward acceleration to ascend.


The thrust can be obtained by the various engines,the most commonly used in light weight aircraft is Propeller engine.
Like the fan pushes the air outward and by newton 3rd law is being thrown backward itself, Similarly but in different way, The propeller blades are so twisted so that it sucks the air in as a result by Newton 3rd law, it is forced forward. This is the thrust.

The lift is created on the wing by the beautiful principle of pressure difference of air between the Upper surface and Lower surface of the wing. The pressure below the wing is higher than the Upper surface. So net force is upward, This force is known as lift.
The pressure exerted by the flow depends on the shape of the surface it follow on.The Upper surface is more convex than lower surface.
                                                       Flow over the Wing(2d cross section is shown)

As  a resultant of both the forces( Net lift and Net Thrust) airplane fly in the air.

Love is Energy

The infinite source of energy is love, love has the cure for everything;So love everyone.