PV239 #android2 cv2 Marek Sedlak @msed__ YOU WANT TO SCROLL? LISTVIEW ListView ● A view that shows items in a vertically scrolling list. ● Typical use case is phonebook ● Model -> Adapter -> ViewHolder ListView.setAdapter(adapter); ● More efficient -> RecyclerView (more complicated; try at home) NETWORKING Networking ● AndroidManifest.xml ○ ● Images ○ Glide / Picasso / Fresco ● Http ○ OkHttpClient ○ HttpUrlConnection ● REST ○ Retrofit 2 ● JSON ○ GSON Glide / Picasso / Fresco ● Nice overview ○ http://stackoverflow.com/questions/29363321/picasso-v-s-imageloader-v-s- fresco-vs-glide ● We use Glide ○ https://github.com/bumptech/glide#download ○ https://github.com/bumptech/glide#how-do-i-use-glide ● Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView); ● Nice tutorials ○ https://futurestud.io/tutorials/glide-image-resizing-scaling EXERCISE1 Playing with images. Show Item in the Listview with 120x120 resolution ● Add Glide into Gradle file ● Add ImageView into item layout ● Then ● Glide .with(context) .load(url) .override(120, 120) .fitCenter() .into(imageView); Gson ● Java serialization/deserialization Library by Google ● https://mvnrepository.com/artifact/com.google.code.gson/gson ● Let’s say public class User { public String name; // + getter, setter } ● Then Gson gson = new Gson(); User user = new User(); user.setName(“Fero”); String userJson = gson.toJson(user); User userCopy = gson.fromJson(userJson, User.class) OkHttpClient ● An HTTP & HTTP/2 client for Android and Java applications ● http://square.github.io/okhttp/ ● https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp ● Nice example from Android1 slides: Request request = new Request.Builder() .url(url) .post(RequestBody.create(.., json)) .build(); Response response = client .newCall(request) .execute(); response.body().string(); Retrofit2 - once you hear REST, use this ● A type-safe HTTP client for Android and Java ● https://square.github.io/retrofit/ ● https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit public interface GitHubService { @GET("users/{user}/repos") Call> listRepos(@Path("user") String user); } Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class); Call> repoListCall = service.listRepos("octocat"); repoListCall.enqueue(...); EXERCISE2 Playing with REST. Show avatar from Github username in the ImageView ● API Docs: https://developer.github.com/v3 ○ https://developer.github.com/v3/users/#get-a-single-user ● GSON ● Glide ● Retrofit2 THANK YOU See you on Slack.