Chuyển đến nội dung chính

Cách đọc json (bằng url GET) bằng cách kêt hợp OkHttpClient và Gson

Gỉa sử ta có đừng dẫn json như sau

http://domain.com/qlvb_mobile/api/ds_donvi

Có nội dung json như sau

{
"status": true
"result": "Success"
"data": 
  {
"DV_ID": "10156"
"DV_TEN": "Sở Thông tin và Truyền thông"
}
  {
"DV_ID": "2"
"DV_TEN": "Sở Tài nguyên và Môi trường"
}
  {
"DV_ID": "3"
"DV_TEN": "Sở Giáo dục và Đào tạo"
}
  {
"DV_ID": "4"
"DV_TEN": "Sở Văn hóa - Thể thao và Du lịch"
}

}

Ta chuẩn bị 1 class như sau để nhận kết quả trả về:


import com.google.gson.annotations.SerializedName;
import java.util.List;
import vn.gov.vn.quanlyvanbansonganh.model.DonVi;
public class DanhSachDonViResult {
@SerializedName("status")
public Boolean status;
@SerializedName("result")
public String message;
public List<DonVi> data;
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<DonVi> getData() {
return data;
}
public void setData(List<DonVi> data) {
this.data = data;
}
}
Hàm lấy json trả về

private void LoadData(){
// Khởi tại OkhttpClient để lấy dữ liệu
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(Constant.URL_BASE + Constant.URL_DANH_SACH_DON_VI)
.build();
// Khởi tạo Gson
final Gson gson = new Gson();
// Thư thi Request
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
DanhSachDonViResult danhSachDonViResult = gson.fromJson(json,DanhSachDonViResult.class);
listDonVi = danhSachDonViResult.data;
runOnUiThread(new Runnable() {
@Override
public void run() {
donViAdapter = new DonViAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,listDonVi);
autocompletetextview.setThreshold(1);
autocompletetextview.setAdapter(donViAdapter);
Log.d("listDonVi",listDonVi.toString());
}
});
}
});
}
}

Nhận xét

Bài đăng phổ biến từ blog này

NHỮNG WIDGET THƯỜNG DÙNG TRONG FLUTTER

 https://baoflutter.com/nhung-widget-thuong-dung-trong-flutter/ Trong bài viết trước về  cách xây dựng màn hình ứng dụng Flutter , các bạn biết rằng các màn hình ứng dụng được tạo ra bởi các widget ghép lại với nhau. Vì vậy việc hiểu và sử dụng các Widget là rất quan trọng. Vì vậy trong bài viết này, tôi sẽ giới thiệu cho các bạn về những widget quan trọng trong Flutter. Hầu hết các Widget đều có các phần sau đây: + Đặc tính của Widget như : color, theme, height, weight, decoration, onTap, onPressed + Liên kết với các Widget khác với từ khoá: child, children, home hoặc body Ví dụ : 1 2 3 4 5 6 Container ( color : Colors . blue , height : 300 , weight : 300 , child : Text ( "Widget con" ) , ) Khi làm một số App cơ bản, bạn sẽ nắm chắc được cách sử dụng các Widget hay dùng. MaterialApp – Là widget rất liện lợi, cung cấp các widget cho việc xây dựng ứng dụng sử dụng thư viện Material Design UI của google. – Widget này được sử dụng trong hàm build đầu tiên của hầu hết các ứn...

Get Current location using FusedLocationProviderClient in Android

Hello to coders, Previously we have taught you  how you get current location using GPS/Network Provider . Then android has revealed  FusedLocationProviderClient  under  GoogleApi . FusedLocationProviderClient is for interacting with the location using fused location provider. ( NOTE :   To use this feature, GPS must be turned on your device. For manually ask the user to turn on GPS, please check  next article ) So let’s get started for the tutorial for getting the current location. First, add a dependency for location by play services: implementation 'com.google.android.gms:play-services-location:15.0.1' Then define FusedLocationProviderClient: private FusedLocationProviderClient mFusedLocationClient; private TextView txtLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main ); this.txtLocation = (TextView) findViewById(R.id. txtLocat...

Get You Last known Location & Current Location using FusedLocationProviderClient

NOVEMBER 30, 2017 Get You Last known Location & Current Location using FusedLocationProviderClient        I would like to cover very basic and simple example of retreiving last known location & Current Location  using Fused location API.  I have written many example for fused location API in my previous posts but in this post i will show how to get retrieve the location  without implementing Google Api Client . All these magics happen after the release of version  11.0.0 of Google Play services SDK. FusedLocationProviderApi is Deprecated We have previously used following way to retrieve the last known location. mLastLocation  =  LocationServices. FusedLocationApi . getLastLocation ( mGoogleApiClient ) ; We no need to define LocationServices. FusedLocationApi  anymore. it is deprecated. It will be removed in the future release. Instead You can use LocationServi...