Introduction:
- Creating new project.
- Setting up the project with Fuel HTTP.
- Implementing the Fuel HTTP Service Call.
- Open Android Studio and Select Create new project.
- Name the project as your wish and tick the Kotlin checkbox support.
- Then Select your Activity type (For Example: Navigation Drawer Activity, Empty Activity, etc.).
- Then Click “finish” button to create new project in Android Studio

implementation 'com.github.kittinunf.fuel:fuel-android:1.6.0'
compile 'com.github.kittinunf.fuel:fuel-android:1.6.0'
Step 3: Fuel HTTP implementation:
<uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidmads.kotlinfuelhttpsample.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="GET Request's Response"
android:textColor="#000000" />
<TextView
android:id="@+id/tvGetResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#e1e1e1"
android:padding="10dp"
android:text="" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimary"
android:onClick="httpGetJson"
android:text="GET RESPONSE"
android:textColor="#ffffff" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="POST Request's Response"
android:textColor="#000000" />
<TextView
android:id="@+id/tvPostResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#e1e1e1"
android:padding="10dp"
android:text="" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimary"
android:onClick="httpPostJson"
android:text="POST RESPONSE"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
FuelManager.instance.basePath = "http://demosmushtaq.16mb.com";
Fuel.get("<Service Link without base path>")
.responseJson { request, response, result ->
Log.v(“response”, result.get().content)
}
Fuel.post("<Service Link without base path>", listOf("<key>" to "<value>"))
.responseJson { request, response, result ->
Log.v(“response”, result.get().content)
}
Full Code of MainActivity:
class MainActivity : AppCompatActivity() {
var tvGetResponse: TextView? = null
var tvPostResponse: TextView? = null
var progress: ProgressDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViewsAndWidgets()
FuelManager.instance.basePath = "http://demosmushtaq.16mb.com";
}
private fun initViewsAndWidgets() {
tvGetResponse = findViewById(R.id.tvGetResponse)
tvPostResponse = findViewById(R.id.tvPostResponse)
progress = ProgressDialog(this)
progress!!.setTitle("Kotlin Fuel Http Sample")
progress!!.setMessage("Loading...")
}
fun httpGetJson(view: View) {
try {
progress!!.show()
Fuel.get("api/get_sample.php").responseJson { request, response, result ->
tvGetResponse!!.text = result.get().content
}
} catch (e: Exception) {
tvGetResponse!!.text = e.message
} finally {
progress!!.dismiss()
}
}
fun httpPostJson(view: View) {
try {
progress!!.show()
Fuel.post("api/post_sample.php", listOf("version_index" to "1")).responseJson { request, response, result ->
tvPostResponse!!.text = result.get().content
}
} catch (e: Exception) {
tvPostResponse!!.text = e.message
} finally {
progress!!.dismiss()
}
}
}
Follow Us
Were this world an endless plain, and by sailing eastward we could for ever reach new distances