การรับส่งข้อมูลจากหน้าจอกับ server แบบ JSON ใช้กับ Retrofit

Ponglang Petrung
Jan 17 · 7 min read
dependencies {


compile "com.android.support:design:26.1.0"
compile "com.android.support:recyclerview-v7:26.1.0"
compile "com.android.support:cardview-v7:26.1.0"
//Retrofit Dependencies
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exmple.recyclerviewretrofitserverjsonkukps">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.exmple.recyclerviewretrofitserverjsonkukps.model;import java.util.List;
public class MovieResponse{
private int page;
private int totalPages;
private List<ResultsItem> results;
private int totalResults;
public void setPage(int page){
this.page = page;
}
public int getPage(){
return page;
}
public void setTotalPages(int totalPages){
this.totalPages = totalPages;
}
public int getTotalPages(){
return totalPages;
}
public void setResults(List<ResultsItem> results){
this.results = results;
}
public List<ResultsItem> getResults(){
return results;
}
public void setTotalResults(int totalResults){
this.totalResults = totalResults;
}
public int getTotalResults(){
return totalResults;
}
}
package com.exmple.recyclerviewretrofitserverjsonkukps.model;
import java.util.List;
public class ResultsItem{
private String overview;
private String originalLanguage;
private String originalTitle;
private boolean video;
private String title;
private List<Integer> genreIds;
private String posterPath;
private String backdropPath;
private String releaseDate;
private double voteAverage;
private double popularity;
private int id;
private boolean adult;
private int voteCount;
public void setOverview(String overview){
this.overview = overview;
}
public String getOverview(){
return overview;
}
public void setOriginalLanguage(String originalLanguage){
this.originalLanguage = originalLanguage;
}
public String getOriginalLanguage(){
return originalLanguage;
}
public void setOriginalTitle(String originalTitle){
this.originalTitle = originalTitle;
}
public String getOriginalTitle(){
return originalTitle;
}
public void setVideo(boolean video){
this.video = video;
}
public boolean isVideo(){
return video;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setGenreIds(List<Integer> genreIds){
this.genreIds = genreIds;
}
public List<Integer> getGenreIds(){
return genreIds;
}
public void setPosterPath(String posterPath){
this.posterPath = posterPath;
}
public String getPosterPath(){
return posterPath;
}
public void setBackdropPath(String backdropPath){
this.backdropPath = backdropPath;
}
public String getBackdropPath(){
return backdropPath;
}
public void setReleaseDate(String releaseDate){
this.releaseDate = releaseDate;
}
public String getReleaseDate(){
return releaseDate;
}
public void setVoteAverage(double voteAverage){
this.voteAverage = voteAverage;
}
public double getVoteAverage(){
return voteAverage;
}
public void setPopularity(double popularity){
this.popularity = popularity;
}
public double getPopularity(){
return popularity;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setAdult(boolean adult){
this.adult = adult;
}
public boolean isAdult(){
return adult;
}
public void setVoteCount(int voteCount){
this.voteCount = voteCount;
}
public int getVoteCount(){
return voteCount;
}
}
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
package com.exmple.recyclerviewretrofitserverjsonkukps.network;


import com.exmple.recyclerviewretrofitserverjsonkukps.model.MovieItem;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.Call;


/**
* Created by Gino Osahon on 13/03/2017.
*/
public interface MovieApiService {
@GET("movie/top_rated")
Call<MovieItem> getTopRatedMovies(@Query("api_key") String apiKey);

}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movies_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="72dp"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/movie_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingRight="16dp"
android:src="@drawable/vdo_two" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:textStyle="bold"
android:text="data1"
android:textSize="16sp" />


<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:text="data1"
/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:paddingRight="16dp"
android:text="data1"
/>
</LinearLayout>

</LinearLayout>
package layout;
import android.content.Context;
import android.graphics.Movie;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.exmple.recyclerviewretrofitserverjsonkukps.R;
import com.exmple.recyclerviewretrofitserverjsonkukps.model.MovieItem;
import com.squareup.picasso.Picasso;
import java.util.List;
/**
* Created by Pongploy2017 on 1/13/2018.
*/
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieViewHolder> {
private List<MovieItem> movies;
private int rowLayout;
private Context context;
public static final String IMAGE_URL_BASE_PATH = "http://image.tmdb.org/t/p/w342//";
public MovieAdapter(List<MovieItem> movies, int rowLayout, Context context) {
this.movies = movies;
this.rowLayout = rowLayout;
this.context = context;
}
//A view holder inner class where we get reference to the views in the layout using their ID
public static class MovieViewHolder extends RecyclerView.ViewHolder {
LinearLayout moviesLayout;
TextView movieTitle;
TextView data;
TextView movieDescription;
ImageView movieImage;
public MovieViewHolder(View v) {
super(v);
moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
movieImage = (ImageView) v.findViewById(R.id.movie_image);
movieTitle = (TextView) v.findViewById(R.id.title);
data = (TextView) v.findViewById(R.id.date);
movieDescription = (TextView) v.findViewById(R.id.description);
}
}


@Override
public MovieAdapter.MovieViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new MovieViewHolder(view);
}
@Override
public void onBindViewHolder(MovieViewHolder holder, final int position) {
String image_url = IMAGE_URL_BASE_PATH + movies.get(position).getPosterPath();
Picasso.with(context)
.load(image_url)
.placeholder(android.R.drawable.sym_def_app_icon)
.error(android.R.drawable.sym_def_app_icon)
.into(holder.movieImage);
holder.movieTitle.setText(movies.get(position).getTitle());
holder.data.setText(movies.get(position).getReleaseDate());
holder.movieDescription.setText(movies.get(position).getOverview());
}
@Override
public int getItemCount() {
return movies.size();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Android Open Source Projects [inclusion] [SUM] [Join user groups] There is a problem with Android projects and want to give answers. What's new or want to update the robot continuously. Can join. Line: po56789 or inbox on the page, come to join, please just !!!!
Thank you

Ponglang Petrung

Written by

Senior Android Devaloper Freewill Solutions Co.,Ltd.) FB Group https://www.facebook.com/groups/883546485084033/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade