0

I am developing an android app (minSdkVersion: 23). In my signIn method, I make a call like that:

RequestClass.doRequestWithApi(this.getApplicationContext(), this.TAG, dataToPass, this::getMyAccount);

And here is the doRequestWithApi method:

public static void doRequestWithApi(Context c, final String TAG, final Map<String, String> paramsToPass, final Function<JSONObject,Boolean> f){

    String urlData = "http://...";
    //For localhost
    //String urlData = "http://10.0.2.2/...";

    Log.i(CAT,paramsToPass.toString());

    URL url = null;
    try {

        url = new URL(urlData);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        url = uri.toURL();

        Log.i(CAT, "Start Building");

        StringRequest jsObjRequest = new StringRequest(Request.Method.POST, url.toString(),
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i(CAT, "Reponse : " + response.toString() );
                        try {
                            f.apply(new JSONObject(response));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i(TAG, error.toString());
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Language", Locale.getDefault().toString());
                return map;
            }

            @Override
            protected Map<String, String> getParams(){
                return paramsToPass;
            }

        };

        jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        // Access the RequestQueue through your singleton class.
        MySingletonRequestApi.getInstance(c).addToRequestQueue(jsObjRequest, TAG);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

As you can see, this::getMyAccount is a use of functional interface which is a java 8 functionality.

I found this page: Java 8 Android support It shows that java.lang.FunctionalInterface is only supported on API level 24 or higher. In fact, when I try to run my app on a 6.0 android device (API 23), it throws this error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: etsymbiose.defdress, PID: 8296
              java.lang.NoClassDefFoundError: etsymbiose.defdress.LoginActivity$$Lambda$1
                  at etsymbiose.defdress.LoginActivity.onCreate(LoginActivity.java:51)
                  at android.app.Activity.performCreate(Activity.java:6301)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5728)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

How can I solve this problem?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
fdieval
  • 99
  • 1
  • 6

2 Answers2

0

In your app level build.gradle file, you can specify compileOptions block like such to use Java-8 features:

android {
  ...

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
  • This is not exactly what I have in my build.gradle file. I do have the same compileOptions block but with: targetCompatibility 1.8 sourceCompatibility 1.8 which I guess is the same. – fdieval Jan 10 '18 at 10:12
0

If you are using Android Studio, just go to File -> Project Structure -> app and change Source Compatibility to 1.8 and Target Compatibility also to 1.8 like in the image below: enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193