This repository contains HttpUrlConnection and Volley based application example

Overview

Consuming-API-in-Andriod

This repository contains HttpUrlConnection and Volley based application example. In this you will find implementation of both ways to consume API. This repository show examples to send receive the data in both cases.

DEMO

SendingDataToAPIAndGettingRespnse.mp4

HttpUrlConnection

Fetching data

  1. Checking for internet connectivity using connectivity manager public boolean isDeviceConnected(){ connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo [] networkInfo = connectivityManager.getAllNetworkInfo(); for(NetworkInfo info : networkInfo){ if(info.getState()== NetworkInfo.State.CONNECTED){ return true; }}
  2. Fetching data from an API public String getDataFromAPI(String url_string){ try { URL url = new URL(url_string); HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection(); httpURLConnection.connect(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); while ((line = bufferedReader.readLine())!=null){ readData+=line; } return readData; } catch (MalformedURLException e) { return e.getMessage(); } catch (IOException e) { return e.getMessage(); } }

3.Start the thread and fetch data public void startThreadForGettingDataUsingHttp(){ Thread thread = new Thread(new Runnable() { @Override public void run() { Log.d("network","Loading data"); String data = getDataFromAPI(url_string); Log.d("network",data); } }); thread.start(); }

Volly

  1. Add dependency: com.android.volley:volley:1.2.1

Fetching data

public void getDataUsingVolley(String url_string){ RequestQueue queue = Volley.newRequestQueue(this); StringRequest stringRequest = new StringRequest(Request.Method.GET, url_string, new Response.Listener () { @Override public void onResponse(String response) { Log.d("response",response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("response",error.getMessage()); } }); queue.add(stringRequest); }

POSTING DATA

public void postData(String url_string){ RequestQueue queue = Volley.newRequestQueue(this); StringRequest stringRequest = new StringRequest(Request.Method.POST, url_string, new Response.Listener () { @Override public void onResponse(String response) { Log.d("response",response); response_txt.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("response","Error is : "+error.getMessage()); response_txt.setText(error.getMessage()); } }){ @Nullable @Override protected Map getParams() throws AuthFailureError { HashMap map = new HashMap<>(); map.put("userEmail",userEmail.getText().toString()); map.put("userPassword",userPassword.getText().toString()); map.put("userFirstName",userFirstName.getText().toString()); map.put("userLastName",userLastName.getText().toString()); map.put("userMobile",userMobile.getText().toString()); return map; } }; queue.add(stringRequest); }

You might also like...

A simple banking system written in Java used to teach object-oriented programming and best coding practices.

didactic-bank-application A simple banking system written in Java used to teach object-oriented programming and best coding practices. It is a three-l

Aug 28, 2022

🗺 Preenchimento automático de endereço através do CEP, consumindo uma API com Volley.

🗺 Preenchimento automático de endereço através do CEP, consumindo uma API com Volley.

Consulta de CEP para Android 🗺 Preenchimento automático de endereço através do CEP, consumindo uma API com Java e Volley. Saiba mais sobre a API ViaC

Jan 26, 2022

This repository contains a functional example of an order delivery service similar to UberEats, DoorDash, and Instacart.

Order Delivery Microservice Example In an event-driven microservices architecture, the concept of a domain event is central to the behavior of each se

Dec 7, 2022

This repository is for Todo application. This contains the Backend part of the application.

Todo Application 개요(Abstract) 개인용 할일 목록 리스트 앱플리케이션 구축 (Personal Todo List Application) 목적 1. React.js기초, AWS서버 활용, 스프링 부트 공부 목적으로 프로젝트 시작했습니다.

Jan 8, 2022

This repository contains example codes which will help you to know how to use selenium webdriver.

❓ What is this Repository about? This repo has example codes with Selenium 4 features. Websites used for testing are: automationpractice.com, saucedem

Dec 30, 2022

This project contains a full example of an application developed using Spring Boot and GraphQL within the Java.

Spring boot GraphQL Example This project contains a full example of an application developed using GraphQL within the Java. The project includes a com

Jul 20, 2022

This repository contains examples using TestContainers in a Spring Boot Application

This repository contains examples using TestContainers in a Spring Boot Application

Sample REST CRUD API with Spring Boot, Mysql, JPA and Hibernate Using TestContainer to assure our Integration/API testing Steps to Setup 1. Build and

Nov 29, 2021

Tic-Tac-Toe-GUI - This repository contains Java based interactive Tic-Tac-Toe game.

Tic-Tac-Toe This repository contains Java based interactive Tic-Tac-Toe game. In this game you can play individual or with another player with your na

Sep 1, 2022

This repository contains the code for the Runescape private server project, and this repo is soley maintained by @Avanae and @ThePolyphia and @Xeveral

Runescape: The private server project. A Runescape private server based on the 2009 era. This repository contains the code for the Runescape private s

Oct 1, 2022

An Open-Source repository 🌎 that contains all the Data Structures and Algorithms concepts and their implementation, programming questions and Interview questions

An Open-Source repository 🌎 that contains all the Data Structures and Algorithms concepts and their implementation, programming questions and Interview questions

An Open-Source repository 🌎 that contains all the Data Structures and Algorithms concepts and their implementation, programming questions and Interview questions. The main aim of this repository is to help students who are learning Data Structures and Algorithms or preparing for an interview.

Dec 29, 2022

This repository contains codes for various data structures and algorithms in C, C++, Java, Python, C#, Go, JavaScript and Kotlin.

Overview The goal of this project is to have codes for various data structures and algorithms - in C, C++, Java, Python, C#, Go, JavaScript and Kotlin

Mar 2, 2022

This repository contains all the Data Structures and Algorithms concepts and their implementation in several ways

This repository contains all the Data Structures and Algorithms concepts and their implementation in several ways

An Open-Source repository that contains all the Data Structures and Algorithms concepts and their implementation in several ways, programming questions and Interview questions. The main aim of this repository is to help students who are learning Data Structures and Algorithms or preparing for an interview.

Dec 31, 2022

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

Jan 5, 2023

Spring Boot Security Login example with JWT and H2 example

 Spring Boot Security Login example with JWT and H2 example

Spring Boot Security Login example with JWT and H2 example Appropriate Flow for User Login and Registration with JWT and HttpOnly Cookie Spring Boot R

Dec 21, 2022

This module explains about the example of Spring MVC + Database Integration with MySQL using Hibernate ORM with practical coding example and required JAR dependencies

SpringMVC-Database-Integration This module explains about the example of Spring MVC + Database Integration with MySQL using Hibernate ORM with practic

Nov 2, 2021

A repository that contains Data Structure and Algorithms coded on Java

A repository that contains Data Structure and Algorithms coded on Java . It will also contain solutions of questions from Leetcode.

Oct 15, 2022

This JAVA repository contains solutions for common algorithms and problems.

This JAVA repository contains solutions for common algorithms and problems.

JAVA-Algorithms 🚀 Description Beep Boop! Boop Beep!. I have created this repository to improve my Logical thinking skills & Knowledge in programming.

Apr 11, 2022
Owner
Zeeshan
Zeeshan
🪖 A library for counting queries for Spring Data JPA application

?? Query-Counter Query-Counter is a library for counting queries for Spring Data JPA application, currently supporting Hibernate only. It aims to help

Jinhong 3 Dec 12, 2021
Codigician backend application.

codigician-api Getting Started You can use docker compose to run the application. # go to build directory $ cd build/ # start couchbase container $ do

codigician 2 Nov 17, 2021
Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Microserver A convenient modular engine for Microservices. Microserver plugins offer seamless integration with Spring (core), Jersey, Guava, Tomcat, G

AOL 936 Dec 19, 2022
Rapidoid - Extremely Fast, Simple and Powerful Java Web Framework and HTTP Server!

Rapidoid - Simple. Powerful. Secure. Fast! Rapidoid is an extremely fast HTTP server and modern Java web framework / application container, with a str

null 1.6k Dec 30, 2022
Rest.li is a REST+JSON framework for building robust, scalable service architectures using dynamic discovery and simple asynchronous APIs.

Rest.li is an open source REST framework for building robust, scalable RESTful architectures using type-safe bindings and asynchronous, non-blocking I

LinkedIn 2.3k Dec 29, 2022
A type-safe HTTP client for Android and the JVM

Retrofit A type-safe HTTP client for Android and Java. For more information please see the website. Download Download the latest JAR or grab from Mave

Square 41k Jan 5, 2023
Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API

Swagger Core NOTE: If you're looking for Swagger Core 1.5.X and OpenAPI 2.0, please refer to 1.5 branch. NOTE: Since version 2.1.7 Swagger Core suppor

Swagger 7.1k Jan 5, 2023
Better compatibility, compatible with konas, future, pyro and rusher.

Compatible phobos 1.9.0 compatible with pyro, future, konas and rusher. to build it yourself, use: MACOS/LINUX: ./gradlew setupDecompWorkspace ./gradl

Hurb 15 Dec 2, 2022