OkHttp


 

  1. Get An URL
  2. Get An Image
  3. Post To Server
  4. Authentication

 

Steps to do before:

  1. Download the latest version of OkHttp library ( jar file ) from here and download Okio library ( jar file ) from here.
  2. For Eclipse: Copy the downloaded library file to “libs” directory of the project. Right click on jar file -> Build Path -> Add to Build path.
    For Android Studio: Copy the downloaded library file to “libs” directory of the project structure and add compile files(‘libs/your lib name here’) is in your build.gradle or
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.okio:okio:1.4.0'

 

1. Get An URL

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class OkHttpHandler.java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:

okhttp_string okhttp_string2

Download SourceCode

 

2. Get An Image

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class .java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:

okhttp_image okhttp_image2

Download SourceCode

 

 

3. Post To Server

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class OkHttpHandler.java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:
Download SourceCode

 

 

4. Authentication

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class OkHttpHandler.java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:

okhttp_authen okhttp_authen2 okhttp_authen3

Download SourceCode