Xamarin 绑定错误 CS0534:'HttpGetRequest' 未实现继承的抽象成员 'AsyncTask.DoInBackground(params Object[]?)'

Xamarin Binding Error CS0534: 'HttpGetRequest' does not implement inherited abstract member 'AsyncTask.DoInBackground(params Object[]?)'

我想在我的 Xamarin.Android 项目中使用 Triton Digital Mobile SDK

创建绑定库并尝试构建它后,出现此错误:

error CS0534: 'HttpGetRequest' does not implement inherited abstract member 'AsyncTask.DoInBackground(params Object[]?)'

我使用 decompiler :

检查了 HttpGetRequest 的内容
package com.tritondigital.util;

import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpGetRequest extends AsyncTask<String, Void, String> {
  public static final String REQUEST_METHOD = "GET";
  
  public static final int READ_TIMEOUT = 15000;
  
  public static final int CONNECTION_TIMEOUT = 15000;
  
  protected String doInBackground(String... params) {
    String result, stringUrl = params[0];
    try {
      URL myUrl = new URL(stringUrl);
      HttpURLConnection connection = (HttpURLConnection)myUrl.openConnection();
      connection.setRequestMethod("GET");
      connection.setReadTimeout(15000);
      connection.setConnectTimeout(15000);
      connection.connect();
      connection.getResponseCode();
      InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
      BufferedReader reader = new BufferedReader(streamReader);
      StringBuilder stringBuilder = new StringBuilder();
      String inputLine;
      while ((inputLine = reader.readLine()) != null)
        stringBuilder.append(inputLine); 
      reader.close();
      streamReader.close();
      result = stringBuilder.toString();
    } catch (IOException e) {
      result = null;
    } 
    return result;
  }
  
  protected void onPostExecute(String result) {
    super.onPostExecute(result);
  }
}

这是 Xamarin 绑定库出错的文件内容:

using System;
using System.Collections.Generic;
using Android.Runtime;
using Java.Interop;

namespace Com.Tritondigital.Util {

    // Metadata.xml XPath class reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']"
    [global::Android.Runtime.Register ("com/tritondigital/util/HttpGetRequest", DoNotGenerateAcw=true)]
    public partial class HttpGetRequest : global::Android.OS.AsyncTask {
        // Metadata.xml XPath field reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/field[@name='CONNECTION_TIMEOUT']"
        [Register ("CONNECTION_TIMEOUT")]
        public const int ConnectionTimeout = (int) 15000;

        // Metadata.xml XPath field reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/field[@name='READ_TIMEOUT']"
        [Register ("READ_TIMEOUT")]
        public const int ReadTimeout = (int) 15000;

        // Metadata.xml XPath field reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/field[@name='REQUEST_METHOD']"
        [Register ("REQUEST_METHOD")]
        public const string RequestMethod = (string) "GET";

        static readonly JniPeerMembers _members = new XAPeerMembers ("com/tritondigital/util/HttpGetRequest", typeof (HttpGetRequest));

        internal static IntPtr class_ref {
            get { return _members.JniPeerType.PeerReference.Handle; }
        }

        [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)]
        [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)]
        public override global::Java.Interop.JniPeerMembers JniPeerMembers {
            get { return _members; }
        }

        [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)]
        [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)]
        protected override IntPtr ThresholdClass {
            get { return _members.JniPeerType.PeerReference.Handle; }
        }

        [global::System.Diagnostics.DebuggerBrowsable (global::System.Diagnostics.DebuggerBrowsableState.Never)]
        [global::System.ComponentModel.EditorBrowsable (global::System.ComponentModel.EditorBrowsableState.Never)]
        protected override global::System.Type ThresholdType {
            get { return _members.ManagedPeerType; }
        }

        protected HttpGetRequest (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer)
        {
        }

        // Metadata.xml XPath constructor reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/constructor[@name='HttpGetRequest' and count(parameter)=0]"
        [Register (".ctor", "()V", "")]
        public unsafe HttpGetRequest () : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
        {
            const string __id = "()V";

            if (((global::Java.Lang.Object) this).Handle != IntPtr.Zero)
                return;

            try {
                var __r = _members.InstanceMethods.StartCreateInstance (__id, ((object) this).GetType (), null);
                SetHandle (__r.Handle, JniHandleOwnership.TransferLocalRef);
                _members.InstanceMethods.FinishCreateInstance (__id, this, null);
            } finally {
            }
        }

        static Delegate cb_doInBackground_arrayLjava_lang_String_;
#pragma warning disable 0169
        static Delegate GetDoInBackground_arrayLjava_lang_String_Handler ()
        {
            if (cb_doInBackground_arrayLjava_lang_String_ == null)
                cb_doInBackground_arrayLjava_lang_String_ = JNINativeWrapper.CreateDelegate ((_JniMarshal_PPL_L) n_DoInBackground_arrayLjava_lang_String_);
            return cb_doInBackground_arrayLjava_lang_String_;
        }

        static IntPtr n_DoInBackground_arrayLjava_lang_String_ (IntPtr jnienv, IntPtr native__this, IntPtr native__params)
        {
            var __this = global::Java.Lang.Object.GetObject<global::Com.Tritondigital.Util.HttpGetRequest> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
            var @params = (string[]) JNIEnv.GetArray (native__params, JniHandleOwnership.DoNotTransfer, typeof (string));
            IntPtr __ret = JNIEnv.NewString (__this.DoInBackground (@params));
            if (@params != null)
                JNIEnv.CopyArray (@params, native__params);
            return __ret;
        }
#pragma warning restore 0169

        // Metadata.xml XPath method reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/method[@name='doInBackground' and count(parameter)=1 and parameter[1][@type='java.lang.String...']]"
        [Register ("doInBackground", "([Ljava/lang/String;)Ljava/lang/String;", "GetDoInBackground_arrayLjava_lang_String_Handler")]
        protected virtual unsafe string DoInBackground (params string[] @params)
        {
            const string __id = "doInBackground.([Ljava/lang/String;)Ljava/lang/String;";
            IntPtr native__params = JNIEnv.NewArray (@params);
            try {
                JniArgumentValue* __args = stackalloc JniArgumentValue [1];
                __args [0] = new JniArgumentValue (native__params);
                var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
                return JNIEnv.GetString (__rm.Handle, JniHandleOwnership.TransferLocalRef);
            } finally {
                if (@params != null) {
                    JNIEnv.CopyArray (native__params, @params);
                    JNIEnv.DeleteLocalRef (native__params);
                }
                global::System.GC.KeepAlive (@params);
            }
        }

        static Delegate cb_onPostExecute_Ljava_lang_String_;
#pragma warning disable 0169
        static Delegate GetOnPostExecute_Ljava_lang_String_Handler ()
        {
            if (cb_onPostExecute_Ljava_lang_String_ == null)
                cb_onPostExecute_Ljava_lang_String_ = JNINativeWrapper.CreateDelegate ((_JniMarshal_PPL_V) n_OnPostExecute_Ljava_lang_String_);
            return cb_onPostExecute_Ljava_lang_String_;
        }

        static void n_OnPostExecute_Ljava_lang_String_ (IntPtr jnienv, IntPtr native__this, IntPtr native_result)
        {
            var __this = global::Java.Lang.Object.GetObject<global::Com.Tritondigital.Util.HttpGetRequest> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
            var result = JNIEnv.GetString (native_result, JniHandleOwnership.DoNotTransfer);
            __this.OnPostExecute (result);
        }
#pragma warning restore 0169

        // Metadata.xml XPath method reference: path="/api/package[@name='com.tritondigital.util']/class[@name='HttpGetRequest']/method[@name='onPostExecute' and count(parameter)=1 and parameter[1][@type='java.lang.String']]"
        [Register ("onPostExecute", "(Ljava/lang/String;)V", "GetOnPostExecute_Ljava_lang_String_Handler")]
        protected virtual unsafe void OnPostExecute (string result)
        {
            const string __id = "onPostExecute.(Ljava/lang/String;)V";
            IntPtr native_result = JNIEnv.NewString (result);
            try {
                JniArgumentValue* __args = stackalloc JniArgumentValue [1];
                __args [0] = new JniArgumentValue (native_result);
                _members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
            } finally {
                JNIEnv.DeleteLocalRef (native_result);
            }
        }

    }
}

关于如何解决这个问题有什么想法吗?

您可以打开Transforms文件夹下的Metadata.xml,添加如下代码:

<add-node path="/api/package[@name='com.tritondigital.util']">
  <class abstract="false" deprecated="not deprecated" final="false" name="HttpGetRequest" static="true" visibility="public" extends="java.lang.Object"/>
</add-node>

此外,您可以查看类似案例并阅读有关绑定的文档。

案例:error CS0534: [java class] does not implement inherited abstract member 'AsyncTask.DoInBackground(params Object[])'

文档:https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb#common-paths