5

Android Networking with Volley

 2 years ago
source link: https://dev.to/sajjadali/android-networking-with-volley-coi?signin=true
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. It is available on GitHub.

In this article, We will be creating a small android project using Kotlin. I have chosen Kotlin as it provides faster development compared to java. You can choose java if you want to. The source code is available at GitHub

Process

Step 01

Create an android studio project, and name it whatever you like.

Step 02

Add the following permission in the manifest file since we would be fetching data from the internet:

<uses-permission android:name="android.permission.INTERNET"/>

Step 03

Add the following dependency to your grade file. You may update the version number based on the version of your tool set.

implementation 'com.android.volley:volley:1.2.1'

Step 04

Create a queue that will manage our volley requests. The following line of code would do it for us:

val queue = Volley.newRequestQueue(this)

here, this refers to the current application context. You may include it anywhere in your code. For simplicity add it in onCreate method of your activity.

Step 05

Create a request object. There are four types of requests available in volley:

  1. JsonObjectRequest
  2. JsonArrayRequest
  3. ImageRequest
  4. StringRequest

We would be working with StringRequest. It requires four things from us:

  1. Method: GET, POST, etc.
  2. Source link
  3. Listener for success
  4. Listener for failure

Here's the code

 val request = StringRequest(
            Request.Method.GET,
            "https://jsonplaceholder.typicode.com/users",
            Response.Listener<String> {
                findViewById<TextView>(R.id.textview).text = it
            },
            Response.ErrorListener {Log.d("error",it.toString())}
 )

Step 06

Add the request to the queue object:

 queue.add(request)

Final code

Main Activity

package com.example.volleydemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val queue = Volley.newRequestQueue(this)
        val request = StringRequest(
            Request.Method.GET,
            "https://jsonplaceholder.typicode.com/users",
            Response.Listener<String> {
                findViewById<TextView>(R.id.textview).text = it
            },
            Response.ErrorListener {Log.d("error", it.toString())}
        )

        queue.add(request)
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textview"
            />
    </ScrollView>

</LinearLayout>

Thanks!

Thanks

Recommend

  • 9
    • Github github.com 5 years ago
    • Cache

    GitHub - google/volley

    README.md Volley Volley is an HTTP library that makes networking for Android apps easier and, most importantly, faster. For more infor...

  • 14
    • www.raywenderlich.com 4 years ago
    • Cache

    Android Networking: Fundamentals [SUBSCRIBER]

    Learn About HTTP & Threading Introduction 2:43 Free...

  • 7
    • blog.csdn.net 4 years ago
    • Cache

    Volley 图片加载相关源码解析

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47721631; 本文出自:【张鸿洋...

  • 6

    Android利用Volley异步加载数据(JSON和图片)完整示例 – Android开发中文站MainActivity如下: package androidchina.net.testvolley;import org.json.JSONObject;

  • 7
    • www.androidchina.net 3 years ago
    • Cache

    是时候用NoHttp来替换Volley了

    是时候用NoHttp来替换Volley了 – Android开发中文站你的位置:Android开发中文站 > Android开发 >

  • 2
    • www.androidchina.net 3 years ago
    • Cache

    Android:Volley源码解析

    简单实例 Volley是一个封装HttpUrlConnection和HttpClient的网络通信框架,集AsyncHttpClient和Universal-Image-Loader的优点于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松...

  • 7
    • praveenpenumaka.github.io 2 years ago
    • Cache

    Volley

    Volley February 10, 2016 Open source community has been a great help for me to learn new stuff. Be it the software development process or coding stand...

  • 6

    Android网络请求(4) 网络请求框架Volley Volley是Google在2013年5月15日到17日在旧金山Moscone中心举办网络开发者年会中推出的Android异步网络加载框架和图片加载框架,它特别适合数据体量小且通讯频繁的网络操作...

  • 5
    • www.producthunt.com 2 years ago
    • Cache

    Consult with Me by Volley

    The fastest way to monetize your expertise

  • 7
    • www.cnblogs.com 2 years ago
    • Cache

    Android Volley 基本使用 - AskaJohnny

    Android Volley 基本使用 本篇主要介绍 Google 给Android 平台提供的 Volley 一个 Http请求库 , 齐射! Volley是Google 提供的一个小巧的异步请求库,扩展很强支持okhttp,(默认是 An...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK