【问题标题】:dnsjava library return "no records found"dnsjava 库返回“未找到记录”
【发布时间】:2019-10-31 07:38:08
【问题描述】:

我在我的 android 项目“android studio”中使用 dnsjava 我已经导入了库。但是当我运行代码时,我得到“没有找到记录”,有人面对这个吗?我在这里找到了一篇讨论类似问题的帖子,但解决方案不起作用。当我将查询类型更改为 ANY 或 A 或 NS 时。它只是行不通。有人知道这是否是由于图书馆代码故障造成的吗?欢迎提出任何建议。

package org.pctechtips.netdroid.async;

import android.os.AsyncTask;
import android.util.Log;

import org.pctechtips.netdroid.response.DnsAsyncResponse;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

import java.lang.ref.WeakReference;

public class DnsLookupAsyncTask extends AsyncTask<String, Void, String> {

    private final WeakReference<DnsAsyncResponse> delegate;

    /**
     * Constructor to set the delegate
     *
     * @param delegate Called when the DNS lookup finishes
     */
    public DnsLookupAsyncTask(DnsAsyncResponse delegate) {
        this.delegate = new WeakReference<>(delegate);
    }

    /**
     * Performs the appropriate lookup for specified record type
     *
     * @param params
     * @return DNS answer
     */
    @Override
    protected String doInBackground(String... params) {
        String domain = params[0];
        int recordType = Integer.parseInt(params[1]);
        Record[] records;

        try {
            records = new Lookup("google.com", Type.ANY).run();
            Log.d("DNS", records.toString());
            if (records == null) {
                return "No records found.";
            }

            StringBuilder answer = new StringBuilder();
            for (Record record : records) {
                String rClass = this.parseRecordClass(record.getDClass());
                answer.append(String.format("%s\t\t\t\t%s\t\t\t\t%s\t\t\t\t%s%n%n", record.getName(), record.getTTL(), rClass, record.rdataToString()));
            }

            return answer.toString();
        } catch (TextParseException e) {
            return "Error performing lookup!";
        }
    }

    /**
     * Determines the string representation of the DNS record class
     *
     * @param recordClass Numeric record class
     * @return Human readable record class
     */
    private String parseRecordClass(int recordClass) {
        switch (recordClass) {
            case 1:
                return "IN";
            case 2:
                return "CS";
            case 3:
                return "CH";
            case 4:
                return "HS";
            default:
                return "IN";
        }
    }

    /**
     * Calls the delegate when the DNS lookup has finished
     *
     * @param result DNS answer
     */
    @Override
    protected void onPostExecute(String result) {
        DnsAsyncResponse activity = delegate.get();
        if (activity != null) {
            activity.processFinish(result);
        }
    }
}

【问题讨论】:

    标签: android dnsjava


    【解决方案1】:

    Android Oreo 进行了一项更改,阻止查找 dns 服务器。 https://developer.android.com/about/versions/oreo/android-8.0-changes.html#o-pri

    有关更多详细信息和解决方法,请参阅:https://stackoverflow.com/a/48973823/5420880

    作为 dnsjava 的特定解决方法,您可以通过 dns.server 系统属性提供以逗号分隔的 dns 服务器列表。

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2021-07-23
      • 2020-08-08
      • 1970-01-01
      • 2020-10-28
      • 2013-07-27
      相关资源
      最近更新 更多