top_5_android_libraries

Har bir android Dasturchi bilishi kerak bo’lgan muhim 5 ta kutubxona(library).

Hozirgi kunga kelib android dastrulashni Android Studio va Gradle larsiz tasavur qilish qiyin bo’lib qoldi. Bundan tashqari biz har doim foydalanadigan juda muhim bir necha ochiq kutubxonalar bor. Lekin biz kerakli kutubxona deb hisoblagan 5tasi bilan ishlash va ularni linklarni ko’rib chiqamiz.

  1. GSON

GSON shunday java kutubxonasiki u bilan biz json formatini java obyektiga o’girishimiz(serializ) va qayta o’girishimiz(deserializ) mumkin.

Asosan API orqali kelgan ma’lumotlarni tahrirlashda ishlatishga juda qulay. JSONni ko’p ishlatilishiga sabab uni kam hajmligi va XML dan ko’ra ancha qulayligida.

  1. // Serialize
  2.  
  3. String userJSON = new Gson().toJson(user);
  4.  
  5. // Deserialize
  6.  
  7. User user = new Gson().fromJson(userJSON, User.class);

  1. RETROFIT

API bilan ishlaydigan projekt uchun eng oqilona tanlangan kutubxona hisoblanadi. API ga yuboriladigan so’rov metodi va URL lar anotatiya qilib yoziladi, bu yozilayotgan dasturni sodda va tushinarli bo’lishiga yordam beradi.

Anotatsiya orqali parametr, so’rov header larni qo’shib yozish imkoni ham mavjud.

API ga murojaat sinxron bajariladi yoki asinxron yuborish mumkin, yani so’rov yuborilgandan keyin natija muvaffaqiyatli(success) yoki xatolik(failure) bo’lib tarzda qaytadi.

  1. public interface RetrofitInterface {
  2.  
  3. // asinxron callback natija qaytaradi
  4.  
  5. @GET("/api/user")
  6.  
  7. User getUser(@Query("user_id") int userId, Callback<User> callback);
  8.  
  9. // sinxorn post qilish
  10.  
  11. @POST("/api/user/register")
  12.  
  13. User registerUser(@Body User user);}
  14.  
  15. }
  16.  
  17. // masalan
  18.  
  19. RetrofitInterface retrofitInterface = new RestAdapter.Builder()
  20.  
  21. .setEndpoint(API.API_URL).build().create(RetrofitInterface.class);
  22.  
  23. // id si 2048 ga teng userni qaytaradi
  24.  
  25. retrofitInterface.getUser(2048, new Callback<User>() {
  26.  
  27. @Override
  28.  
  29. public void success(User user, Response response) {
  30.  
  31. }
  32.  
  33. @Override
  34.  
  35. public void failure(RetrofitError retrofitError) {
  36.  
  37. }});
  38.  
  39. User user = getUser();
  40.  
  41. retrofitInterface.registerUser(user);

Retrofit Gson kutubxonasini default xolatda ishlatib jsonga o’girishi yoki qaytib java objectiga qaytaradi.

  1. EventBus

Eventbus kutubxonasi dasturni turli qismlarini bog’lanishlarni soddalashtiradi. Masalan, Serviceni ishga tushirish uchun Activity orqali nimadir yuborish yoki fragmentlarni bir-biriga o’zaro ta’sir o’tqazishi. Tushunarli bo’lishi uchun agar internet uzilib qolganda activityda chiqarishni misol tariqasida ko’ramiz:

  1. public class NetworkStateReceiver extends BroadcastReceiver {
  2.  
  3. // internet yo’q bulganda post junatadigan evet
  4.  
  5. public void onReceive(Context context, Intent intent) {
  6.  
  7. super.onReceive(context, intent);
  8.  
  9. if(intent.getExtras()!=null) {
  10.  
  11. NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
  12.  
  13. if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
  14.  
  15. // demak internet bor
  16.  
  17. } else if(intent
  18.  
  19. .getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
  20.  
  21. // demak internet yo’’q, internet holatini jo’natamiz
  22.  
  23. EventBus.getDefault().post(new NetworkStateChanged(false));
  24.  
  25. }}
  26.  
  27. // Hodisa (event)
  28.  
  29. public class NetworkStateChanged {
  30.  
  31. private mIsInternetConnected;
  32.  
  33. public NetworkStateChanged(boolean isInternetConnected) {
  34.  
  35. this.mIsInternetConnected = isInternetConnected;
  36.  
  37. }
  38.  
  39. public boolean isInternetConnected() {
  40.  
  41. return this.mIsInternetConnected;
  42.  
  43. }}
  44.  
  45. public class HomeActivity extends Activity {
  46.  
  47. @Override
  48.  
  49. protected void onCreate(Bundle savedInstanceState) {
  50.  
  51. super.onCreate(savedInstanceState);
  52.  
  53. setContentView(R.layout.activity_main);
  54.  
  55. EventBus.getDefault().register(this); // EventBus ni registratisya qilish
  56.  
  57. }
  58.  
  59. @Override
  60.  
  61. protected void onDestroy() {
  62.  
  63. super.onDestroy();
  64.  
  65. EventBus.getDefault().unregister(this); // EventBus ni registratsyadan o’chirish(bu app yopilganda eventbus ni ham eshitib turishini o’chiradi)
  66.  
  67. }
  68.  
  69. // qayerdandir NetworkStateChanged hodisasini post qilinganda ishga tushadigan metod
  70.  
  71. public void onEventMainThread(NetworkStateChanged event) {
  72.  
  73. if (!event.isInternetConnected()) {
  74.  
  75. Toast.makeText(this, "No Internet connection!", Toast.LENGTH_SHORT).show();
  76.  
  77. }
  78.  
  79. }
  80.  
  81. }

  1. ACTIVEANDROID

ActiveAndroid bu android uchun ORM hisoblanadi. Sqlite bilan ishlashini ancha soddalashtiradi yani siz sql so’rovlarsiz ham sqlite bilan ishlash imkoniyatini beradi. Activeandroid dan nasil(extends) olgan model object ma’lumotlar bazasiga shu ko’rinishda saqlaydi:

user.save();

Ya’ni siz uzundan-uzun sql so’rov yozishdan qutilasiz:

INSERT INTO Users (Ism, Familya, Address, Shahar, Davlat) VALUES (Toshmat,Eshmatov,Bodomzor 5,Toshkent,‘Uzbekistan);

Barcha userlarni bazadan olish uchun:

List<User> users = new Select().from(User.class).execute();

Ya’ni sqldagi ko’rinishi:

SELECT Ism, Familya, Address, Shahar, Davlat FROM Users;

  1. Universal Image Loader(UIL)

UIL bu asinxron rasmlarni yuklash va keshlashni oson yechimi. Ishlatishga ham juda qulay hisoblanadi.

imageLoader.displayImage(imageUri, imageView);

Picasso ham ancha qulay hisoblanishiga qaramay, unda sozlashda kamchiliklar bor. UIL da sozlash qurilmasi mavjud, u har qanday rasmni yuklashini yoki keshlashni ayniqsa katta hajmdagi rasmlar bilan ishlay oladi(Picasso katta hajmdagi rasmlar yuklay olmaydi).

Yuqorida ko’rib chiqqan kutubxonalarimizni Android studio yuklash quyidagicha:

  1. dependencies {
  2.  
  3. compile 'com.google.code.gson:gson:2.2.4'
  4.  
  5. compile 'com.squareup.okhttp:okhttp:1.3.0'
  6.  
  7. compile 'com.squareup.retrofit:retrofit:1.3.0'
  8.  
  9. compile 'de.greenrobot:eventbus:2.2.+'
  10.  
  11. compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1'}

MANBA: https://infinum.co sayti

(828 marta o'qilgan, bugun 1 marta o'qildi)

O'xshash maqolalar:

  • Olimhon

    Rahmat juda foydali ma’lumotlar ekan