https://github.com/devdhera/guide-to-json-with-android
JSON parsing demo using Android application
https://github.com/devdhera/guide-to-json-with-android
android example json parsing
Last synced: 5 months ago
JSON representation
JSON parsing demo using Android application
- Host: GitHub
- URL: https://github.com/devdhera/guide-to-json-with-android
- Owner: DevDHera
- License: mit
- Created: 2019-01-06T06:51:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-06T11:42:03.000Z (about 7 years ago)
- Last Synced: 2025-03-30T23:15:35.663Z (10 months ago)
- Topics: android, example, json, parsing
- Language: Java
- Size: 170 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSON Parsing - Android Guide
## Overview
This projects focus on providing a basis for dealing with `JSON` responses that get when using a `REST API`.
Project showcase the JSON parsing using a simple list view based Android Application.
## Content
Project divided into three sections like below.
* [JSON Parsing - Standard](#json-parsing---standard)
* [JSON Parsing - Using Volly](#json-parsing---using-volly)
* [JSON Parsing - Using Retrofit](#json-parsing---using-retrofit)
#### JSON Parsing - Standard
Here we use a separate `HttpHandler` class to modularize and organize our HttpUrlConnections.
```java
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
```
**[Source](https://github.com/DevDHera/Guide-to-JSON-with-Android/tree/master/app)**
#### JSON Parsing - Using Volly
Here we use a separate `AppController` singleton class to initialize volly objects.
```java
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
```
**[Source](https://github.com/DevDHera/Guide-to-JSON-with-Android/tree/master/jsonparsing-volly)**
#### JSON Parsing - Using Retrofit
One of the favourite ways to deal with the Networks is to use the `Retrofit Library`.
We love :heart: Retrofit because its type-safe nature.
Following is how we create a Retrofit instance.
```java
public class ApiClient {
public static final String BASE_URL = "https://api.androidhive.info/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
```
**[Source](https://github.com/DevDHera/Guide-to-JSON-with-Android/tree/master/jsonparsing-retrofit)**