Java for Android

Prérequis:

  1. Ajouter la librairie Fast Networking à Android Studio en modifiant le fichier build.gradle.
    implementation ‘com.amitshekhar.android:android-networking:1.0.2’
  2. Ne pas oublier de rajouter dans le Manifest les autorisations Internet: uses-permission android:name=”android.permission.INTERNET”
  3. Ajouter dans la méthode onCreate() de la classe Application :
    AndroidNetworking.initialize(getApplicationContext());
OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .authenticator(new Authenticator() {
            @Override            
            public Request authenticate(Route route, Response response) throws IOException {
                return response.request().newBuilder()
                        .header("Authorization", Credentials.basic("admin", "yourpassword"))
                        .build();
            }
        })
        .build();


AndroidNetworking.get("http://xxx.pblsrv.com:1337/PBL_SwitchOnPosition/")
                 .addQueryParameter("Position", "POSITION01")
				 .addQueryParameter("Color", "#FFFF00")
                 .setOkHttpClient(okHttpClient)
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });