
1.สร้าง project
1.1 ไปที่ File => New Project ใส่ชื่อ project และ Company domain เลือก Empty Activity
1.2 ไฟล์ build.gradle (Module: app) ใส่
1 2 3 4 5 6 7 8 9 10 11 12 13 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.1' testCompile 'junit:junit:4.12' // retrofit, gson compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' } |
1.3 ไฟล์ AndroidManifest.xml ขอ permission Internet
1 |
<uses-permission android:name="android.permission.INTERNET"/> |
1.4 สร้าง package model กับ rest ใน main package
2. สร้าง Model
จากครั้งที่แล้ว Lumen สร้าง REST API Thai Crane Company เราจะได้ api ที่พร้อมใช้งานแบบนี้ ครับ
2.1 สร้าง Model Company ใน package model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
package co.appxar.thaicrane.model; import com.google.gson.annotations.SerializedName; public class Company { @SerializedName("company_id") private int company_id; @SerializedName("company_name") private String company_name; @SerializedName("company_owner") private String company_owner; @SerializedName("company_address") private String company_address; @SerializedName("company_office_tel") private String company_office_tel; @SerializedName("company_mobile") private String company_mobile; @SerializedName("company_fax") private String company_fax; @SerializedName("company_email") private String company_email; @SerializedName("company_website") private String company_website; @SerializedName("company_lat") private String company_lat; @SerializedName("company_lng") private String company_lng; @SerializedName("company_image_url") private String company_image_url; @SerializedName("province_id") private String province_id; public int getCompany_id() { return company_id; } public void setCompany_id(int company_id) { this.company_id = company_id; } public String getCompany_name() { return company_name; } public void setCompany_name(String company_name) { this.company_name = company_name; } public String getCompany_owner() { return company_owner; } public void setCompany_owner(String company_owner) { this.company_owner = company_owner; } public String getCompany_address() { return company_address; } public void setCompany_address(String company_address) { this.company_address = company_address; } public String getCompany_office_tel() { return company_office_tel; } public void setCompany_office_tel(String company_office_tel) { this.company_office_tel = company_office_tel; } public String getCompany_mobile() { return company_mobile; } public void setCompany_mobile(String company_mobile) { this.company_mobile = company_mobile; } public String getCompany_fax() { return company_fax; } public void setCompany_fax(String company_fax) { this.company_fax = company_fax; } public String getCompany_email() { return company_email; } public void setCompany_email(String company_email) { this.company_email = company_email; } public String getCompany_website() { return company_website; } public void setCompany_website(String company_website) { this.company_website = company_website; } public String getCompany_lat() { return company_lat; } public void setCompany_lat(String company_lat) { this.company_lat = company_lat; } public String getCompany_lng() { return company_lng; } public void setCompany_lng(String company_lng) { this.company_lng = company_lng; } public String getCompany_image_url() { return company_image_url; } public void setCompany_image_url(String company_image_url) { this.company_image_url = company_image_url; } public String getProvince_id() { return province_id; } public void setProvince_id(String province_id) { this.province_id = province_id; } } |
2.2 สร้าง ComanyResponse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package co.appxar.thaicrane.model; import com.google.gson.annotations.SerializedName; import java.util.List; public class CompanyResponse { @SerializedName("results") private List<Company> results; public List<Company> getResults() { return results; } public void setResults(List<Company> results) { this.results = results; } } |
3. สร้าง Retrofit instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package co.appxar.thaicrane.rest; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by devmaster on 6/3/2017 AD. */ public class ApiClient { public static final String BASE_URL = "http://appxar.co/thaicrane/api/v1/" ; public static Retrofit retrofit = null ; public static Retrofit getClient(){ if(retrofit==null){ retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build() ; } return retrofit ; } } |
4. กำหนด ApiInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package co.appxar.thaicrane.rest; import co.appxar.thaicrane.model.Company; import co.appxar.thaicrane.model.CompanyResponse; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; import retrofit2.http.Query; public interface ApiInterface { @GET("company") Call<CompanyResponse> getCompanies(@Query("api_key") String apiKey); @GET("company/{id}") Call<CompanyResponse> getCompany(@Path("id") int id, @Query("api_key") String apiKey); @GET("company/geo/{id}") Call<CompanyResponse> getCompaniesByGeo(@Path("id") int id, @Query("api_key") String apiKey); } |
5. Call company request
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package co.appxar.thaicrane; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import java.util.List; import co.appxar.thaicrane.model.Company; import co.appxar.thaicrane.model.CompanyResponse; import co.appxar.thaicrane.rest.ApiClient; import co.appxar.thaicrane.rest.ApiInterface; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class MainActivity extends AppCompatActivity { public static String API_KEY =""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textView = (TextView) findViewById(R.id.textview1); ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); Call<CompanyResponse> call = apiService.getCompanies(API_KEY) ; call.enqueue(new Callback<CompanyResponse>() { @Override public void onResponse(Call<CompanyResponse> call, Response<CompanyResponse> response) { List<Company> companies = response.body().getResults(); //Log.e("companies",companies.get(0).getCompany_name()); textView.setText(companies.get(0).getCompany_name()); } @Override public void onFailure(Call<CompanyResponse> call, Throwable t) { Log.e("onFailure", t.toString()); } }); } } |
ผลลัพธ์ที่ได้
reference
– http://square.github.io/retrofit/
– http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/
– http://www.pratikbutani.com/2016/05/android-tutorial-json-parsing-using-retrofit-part-1/