Created
September 30, 2021 11:32
-
-
Save kazuiains/526e77ed21dac8059644e3f753493e17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ApiConfig { | |
| private static final String BASE_URL = "urlxxx/api/"; | |
| private static Retrofit retrofit; | |
| public static Retrofit getClient(){ | |
| if (retrofit == null){ | |
| retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| } | |
| return retrofit; | |
| } | |
| public static ApiService getService(){ | |
| Retrofit retrofit = getClient(); | |
| return retrofit.create(ApiService.class); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface ApiService { | |
| @FormUrlEncoded | |
| @POST("register") | |
| Call<RegisterResponse> createAccount(@Field("is_admin") int isAdmin, | |
| @Field("name") String name, | |
| @Field("address") String address, | |
| @Field("phone") String phone, | |
| @Field("email") String email, | |
| @Field("password") String password); | |
| @GET("product") | |
| Call<ProductResponse>getProduct(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DetailProductActivity extends AppCompatActivity { | |
| private TextView tvName, tvCategory, tvStock, tvPrice, tvDesc; | |
| private ApiService service; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_detail_product); | |
| init(); | |
| initApi(); | |
| } | |
| private void init(){ | |
| tvName = findViewById(R.id.nama_produk); | |
| tvCategory = findViewById(R.id.kategori); | |
| tvStock = findViewById(R.id.stok); | |
| tvPrice = findViewById(R.id.harga); | |
| tvDesc = findViewById(R.id.description_produk); | |
| service = ApiConfig.getService(); | |
| } | |
| private void initApi(){ | |
| Call<ProductResponse> client = service.getProduct(); | |
| client.enqueue(new Callback<ProductResponse>() { | |
| @Override | |
| public void onResponse(@NotNull Call<ProductResponse> call, @NotNull Response<ProductResponse> response) { | |
| if (response.isSuccessful) { | |
| ProductResponse respProduct = response.body(); | |
| if((respProduct != null || !respProduct.isEmpty()) && (respProduct.getData() != null || !respProduct.getData().isEmpty())){ | |
| Product dataProduct = respProduct.getData(); | |
| tvName.setText(dataProduct.getName()); | |
| tvCategory.setText(dataProduct.getCategory()); | |
| tvStock.setText(dataProduct.getStock()); | |
| tvPrice.setText(dataProduct.getPrice()); | |
| tvDesc.setText(dataProduct.getDescription()); | |
| } | |
| } | |
| } | |
| @Override | |
| public void onFailure(@NotNull Call<ProductResponse> call, @NotNull Throwable t) { | |
| // Terpanggil ketika tidak dapat menghubungi server | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Product { | |
| @SerializedName("id") | |
| private int id; | |
| @SerializedName("category") | |
| private String category; | |
| @SerializedName("name") | |
| private String name; | |
| @SerializedName("description") | |
| private String description; | |
| @SerializedName("stock") | |
| private String stock; | |
| @SerializedName("price") | |
| private String price; | |
| @SerializedName("photo") | |
| private String photo; | |
| @SerializedName("updated_at") | |
| private String updatedAt; | |
| @SerializedName("created_at") | |
| private String createdAt; | |
| @SerializedName("photo_url") | |
| private String photoUrl; | |
| public Product() { | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getCategory() { | |
| return category; | |
| } | |
| public void setCategory(String category) { | |
| this.category = category; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public void setDescription(String description) { | |
| this.description = description; | |
| } | |
| public String getStock() { | |
| return stock; | |
| } | |
| public void setStock(String stock) { | |
| this.stock = stock; | |
| } | |
| public String getPrice() { | |
| return price; | |
| } | |
| public void setPrice(String price) { | |
| this.price = price; | |
| } | |
| public String getPhoto() { | |
| return photo; | |
| } | |
| public void setPhoto(String photo) { | |
| this.photo = photo; | |
| } | |
| public String getUpdatedAt() { | |
| return updatedAt; | |
| } | |
| public void setUpdated_at(String updatedAt) { | |
| this.updatedAt = updatedAt; | |
| } | |
| public String getCreatedAt() { | |
| return createdAt; | |
| } | |
| public void setCreatedAt(String createdAt) { | |
| this.createdAt = createdAt; | |
| } | |
| public String getPhotoUrl() { | |
| return photoUrl; | |
| } | |
| public void setPhotoUrl(String photoUrl) { | |
| this.photoUrl = photoUrl; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ProductResponse { | |
| @SerializedName("status") | |
| private String status; | |
| @SerializedName("message") | |
| private String message; | |
| @SerializedName("data") | |
| private Product data; | |
| public String getStatus() { | |
| return status; | |
| } | |
| public void setStatus(String status) { | |
| this.status = status; | |
| } | |
| public String getMessage() { | |
| return message; | |
| } | |
| public void setMessage(String message) { | |
| this.message = message; | |
| } | |
| public Product getData() { | |
| return data; | |
| } | |
| public void setData(Product data) { | |
| this.data = data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment