项目作者: F4pl0

项目描述 :
F4pl0's Awesome Request Library for Android
高级语言: Java
项目地址: git://github.com/F4pl0/FARLA.git
创建时间: 2019-09-07T11:58:22Z
项目社区:https://github.com/F4pl0/FARLA

开源协议:GNU General Public License v3.0

下载


API

Android Arsenal

FARLA - F4pl0’s Awesome Request Library for Android

Features

  • Lightweight

    So it can fit in every project you want, compressed to <50KB

  • Native

    Supports Java and Kotlin

  • Performance

    Only bottleneck is your connection and server location

  • Simple

    Implements in seconds, period

  • GET, POST, PUT and DELETE Requests

Installation

Add Jitpack.io repository to your build.gradle file:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Add FARLA to your dependencies

  1. dependencies {
  2. implementation 'com.github.F4pl0:FARLA:0.5.0'
  3. }

Add USES-INTERNET permission to your Android Manifest:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. ...
  3. >
  4. <uses-permission android:name="android.permission.INTERNET" ></uses-permission>
  5. ...
  6. </manifest>

Usage

GET Request (Same for DELETE Request)

  1. new FarlaGetRequest(this) // For DELETE Request change to FarlaDeleteRequest
  2. .setURL("https://example.com/get")
  3. .setListener(new FarlaGetRequest.onGetRequestListener() {
  4. @Override
  5. public void onSuccess(String response) {
  6. //Handle the response
  7. }
  8. @Override
  9. public void onFailure(int error) {
  10. //Handle the failure
  11. }
  12. }).execute();

POST Request (Same for PUT Request)

  1. new FarlaPostRequest(this) // For PUT Request change to FarlaPutRequest
  2. .setURL("https://example.com/post")
  3. .setListener(new FarlaPostRequest.onPostRequestListener() {
  4. @Override
  5. public void onSuccess(String response) {
  6. //Handle the response
  7. }
  8. @Override
  9. public void onFailure(int error) {
  10. //Handle the failure
  11. }
  12. })
  13. .addParam("key", "value")
  14. .execute();

Error Handling

  1. @Override
  2. public void onFailure(int error) {
  3. switch(error){
  4. case Constants.NO_CONNECTION:
  5. // Insert code for handling NO_CONNECTION
  6. // Device has no connection to internet
  7. break;
  8. case Constants.AUTH_FAILURE:
  9. // Insert code for handling AUTH_FAILURE
  10. // Server returned 401, request is not authenticated
  11. // Mabye forgot authentification header?
  12. break;
  13. case Constants.SERVER_ERROR:
  14. // Insert code for handling SERVER_ERROR
  15. // Something happened with the server
  16. // - Check the server-sided code
  17. // - Check the input provided to the server via headers or params
  18. break;
  19. case Constants.NETWORK_ERROR:
  20. // Insert code for handling NETWORK_ERROR
  21. // Request got lost somewhere in the dark woods of the internet
  22. // Mabye network change?
  23. break;
  24. case Constants.PARSE_ERROR:
  25. // Insert code for handling PARSE_ERROR
  26. // - Check URL provided
  27. // - Check Headers provided
  28. // - Check params provided
  29. break;
  30. }
  31. }

Headers

You can add headers with .addHeader(String, String) call:

  1. new FarlaPostRequest(this) // Example
  2. .setURL("https://example.com/post")
  3. .addHeader("Content-Type", "multipart/form-data")
  4. .execute();

NOTE

From Android 9.0+ Google disallows HTTP traffic, only allows secure HTTPS
You will get NO_CONNECTION error on HTTP requests
To allow HTTP Traffic to some servers, you must put them in a network_security_config.xml file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <domain-config cleartextTrafficPermitted="true">
  4. <domain includeSubdomains="true">f4pl0.github.io</domain>
  5. </domain-config>
  6. </network-security-config>

And then include it in the Android Manifest:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest ... >
  3. <application android:networkSecurityConfig="@xml/network_security_config"
  4. ... >
  5. ...
  6. </application>
  7. </manifest>

And you should be allowed to make HTTP requests to the server listed.