如何在24以下的android API下使用(或替换)java 8功能界面?

时间:2018-01-10 08:55:00

标签: android java-8 method-reference functional-interface

我正在开发一个Android应用程序(minSdkVersion:23)。 在我的signIn方法中,我进行了这样的调用:

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

这是doRequestWithApi方法:

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();
    }
}

如您所见,this::getMyAccount是一个功能接口的使用,它是一个java 8功能。

我找到了这个页面:Java 8 Android support 它表明java.lang.FunctionalInterface仅在API级别24或更高级别上受支持。 事实上,当我尝试在6.0 Android设备(API 23)上运行我的应用程序时,它会抛出此错误:

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)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在您的应用级build.gradle文件中,您可以指定compileOptions这样的块来使用Java-8功能:

android {
  ...

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

答案 1 :(得分:0)

如果您使用的是Android Studio,只需转到 void SetComboItem(string id, string value) { Forms.HtmlElement ddRouteSelected = wBrowser.Document.GetElementById(id); foreach (Forms.HtmlElement item in ddRouteSelected.Children) { if (item.InnerText != null && item.InnerText.ToLower().Equals(value.ToLower())) { item.SetAttribute("selected", "Selected"); item.SetAttribute("value", value); ddRouteSelected.InvokeMember("onchange"); break; } } } 和 将File -> Project Structure -> app更改为Source Compatibility,将1.8更改为Target Compatibility,如下图所示: enter image description here