How to connect and run localhost api in android studio

Here in this post, we'll see how to connect or call a localhost endpoint or API when we are developing a real android application.

but before knowing the trick, let's understand what is localhost and how it works?

So our endpoint is nothing but it's a URL.☝☝☝

URL -πŸ‘‰ Uniform Resource Locator

http://localhost:6000/somepath

http - Name of Protocol

localhost - Hostname or Domain Name

6000 - port number (just for example)

somepath - any path which represents your data/task

so if we talk about hostname or domain name - some examples are belowπŸ‘‡πŸ‘‡πŸ‘‡

www.google.com
www.amazon.in

These are hostname or domain names, but actually, they are representing an IP address, 
These domain names have been bound with a specific and unique IP address, as IP address is hard to remember, that's why we got domain name which is very easy to remember.

Here we just have to use these hacks for our use.

In our case, localhost means is that our services are running in our local system on a specific port.

So instead of localhost we just have to use our system's IP address.

Step 1 -

Press windows+R enter cmd and press enter or search for cmd in the windows search area.

Step 2 -

type ipconfig in cmd and press enter.

C:\Users\Asus>ipconfig

Step 3 - 

You will see some information on cmd, 

find the value of IPv4 Address and copy that.

IPv4 Address. . . . . . . . . . . : xxx.xxx.xx.xxx

using x just for example here you'll find some numeric value.

this is your IP address.

So now our endpoint or API looks like  -   

http://localhost:6000/yourpath                    ❌

http://xxx.xxx.xx.xxx:6000/yourpath           ✅

Now just follow some below step and you will be able to access your localhost endpoint while developing an android application.

1 -  Your device and your PC\Laptop should be connected to the same WiFi network.
2 - Define ur base URL in your application code with IP address, not with localhost.
3 - Connect your device with android studio and launch the Application on the device.

 
follow the link below


4- If all is good you'll easily access localhost in your application.

That's all about this post.

Thanks a bunch for being here.
#stayhealthy
#takeCare

*************
******************
*************************



intern() in java

 intern()


public class Javaoneworld {
    public static void main(String args[]) {
      
      String str1 = new String ("javaoneworld"); // 
      String str2  = new String ("javaoneworld");

/*
	 * as we initializing str1 and str2 with new keyword so they will not share the
	 * same memory pool
	 */
      System.out.println(str1 == str2); //false
      
      /*
	 * using the intern() method we can force both the string to share same memory
	 * pool
	 */
      
      str1 = str1.intern();
      str2 = str2.intern();
      
      System.out.println(str1 == str2); //true
    }
}
   







Java: Amazing Interesting and Cool Tricks


Java:  Amazing Interesting and Cool Tricks Part -1 πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡✌

Java☝:: A most beautiful and powerful programming languageπŸ‘Œ.

as much as you'll deep dive into Java you'll learn something new.

java cool tricks


Here are some Amazing and cool trick's - 

1-  

Do you know what will be the output of the below statement? πŸ‘‡

System.out.println(0123); 

o/p = 83   but why?

Click on Below - πŸ‘‡πŸ‘‡πŸ‘‡

System.out.println(0123);



2- Java String

Concatenation - concat() Vs the + Operator 

concat() --

if want to concat str1 with str2 where str1 is null.

str1 = null;
str2 = "javaoneworld";

str1.concat(str2);

you'll get NullPointerExceptionπŸ‘ŽπŸ‘Ž.

+ Operator --

if want to concat str1 with str2 where str1 is null.

str1 = null;
str2 = "javaoneworld";

str1+str2;

you'll not get NullPointerException instead you'll get some outputπŸ˜…πŸ˜€.

o/p - nulljavaoneworld


2-intern()


public class Javaoneworld {
    public static void main(String args[]) {
      
      String str1 = new String ("javaoneworld"); // 
      String str2  = new String ("javaoneworld");

/*
	 * as we initializing str1 and str2 with new keyword so they will not share the
	 * same memory pool
	 */
      System.out.println(str1 == str2); //false
      
      /*
	 * using the intern() method we can force both the string to share same memory
	 * pool
	 */
      
      str1 = str1.intern();
      str2 = str2.intern();
      
      System.out.println(str1 == str2); //true
    }
}
   

2- Comment

As we all know we use comments to not execute the code, 
where we use comment, compiler skips that line.

In java we have two types of comment

//single line comment

/* multi line
comment*/

But what if I want to execute the line which is even commented?

can we achieve this?

Yes , here is a way to achieve thisπŸ‘‡πŸ‘‡



public class Javaoneworld {
    public static void main(String args[]) {
      
     // \u000d System.out.println("Javaoneworld");
     
    }
}
   
This comment will execute successfully because of Unicode character that we appended, java compiler will parses this “\u000d” Unicode character as a new line. Java allows using Unicode characters without encoding.