# Surfing on Android
> Bytedance Android Engineer
hejinhai@bytedance.com
## Points
+ Web Basics
+ Internet
+ HTTP / RESTful API
+ Data-interchange formats
+ Android Practice
+ Parse JSON
+ Fetch Data
Internet (Cloud)
Web Service (C/S)
Network Layers
URL
Uniform Resource Locator
## Request/Response
Client request
```
GET / HTTP/1.1
Host: www.example.com
```
Server response
```
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Content-Type: text/json; charset=UTF-8
Content-Length: 138
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
ETag: "3f80f-1b6-3e1cb03b"
{"message": "hello"}
```
REST Methods
## REST Methods example
| Method | http://x.c/video/ | http://x.c/video/123 |
| ---------------------------- | ------------- | ----- |
| GET | get all videos | get video 123 |
| POST | post video | - |
| PUT | - | update video 123|
| DELETE | delete all videos | delete video 123 |
## REST Documents
+ [REST Wiki](https://en.wikipedia.org/wiki/Representational_state_transfer)
+ [REST API Tutorial](https://restfulapi.net/)
+ [理解RESTful架构](http://www.ruanyifeng.com/blog/2011/09/restful.html)
+ [PostMan](https://www.getpostman.com/)
+ [The Cat API](https://docs.thecatapi.com/)
+ [Github API](https://developer.github.com/v3/)
## Data-interchange formats
Try to represent user info:
+ name(Alice)
+ age(20)
+ languages(zh,en)
JSON vs XML
```json
// JSON - JavaScript Object Notation
{
"name": "Alice",
"age": 20,
"languages": ["zh", "en"]
}
```
```XML
<xml><!-- XML - Extensible Markup Language-->
<name>Alice</name>
<age type="int">20</age>
<languages>
<language>zh</language>
<language>en</language>
</languages>
</xml>
```
JSON data types
```json
{
"string_name": "Alice",
"number_age": 20,
"boolean_vip": true,
"array_languages": ["zh", "en"],
"object_school": {
"name": "Zhejiang University",
"city": "Hangzhou"
},
"null_email": null
}
```
## Parse JSON
+ JSONObject
+ Gson
## JSONObject
[Doc](https://developer.android.com/reference/org/json/JSONObject)
```java
public static String DATA = "{\"name\":\"Sylvia\",\"age\":26}";
JSONObject user = new JSONObject(DATA);
String name = user.getString("name");
int age = user.getInt("age");
```
## Gson
[Github](https://github.com/google/gson)
```java
public static String DATA = "{\"name\":\"Sylvia\",\"age\":26}";
public class User {
@SerializedName("name")
public String name;
@SerializedName("age")
public int age;
}
User user = new Gson().fromJson(DATA, User.class);
String result = user.name;
int age = user.age;
```
## Fetch Data
+ HttpUrlConnection
+ Retrofit
## Permissions
```
<manifest xmlns:tools="http://schemas.android.com/tools"
package="com.bytedance.android.lesson.restapi"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application>
...
</application>
</manifest>
```
## HttpUrlConnection
[Doc](https://developer.android.com/reference/java/net/HttpURLConnection)
```java
String url = "https://api.thecatapi.com/v1/images/search";
// connect
URL netUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) netUrl.openConnection();
// read response data
BufferedInputStream in = new BufferedInputStream(urlConnection.getInputStream());
String data = readStream(in);
// clean up
urlConnection.disconnect();
```
## Retrofit
[Github](https://square.github.io/retrofit/)
```java
// definition
public interface ICNDBService {
@GET("jokes/random") Call randomJoke();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.icndb.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ICNDBService service = retrofit.create(ICNDBService.class);
Response response = service.randomJoke().execute();
Joke joke = response == null ? null : response.body();
```
