package com.sxci.simon.qiniutest;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.qiniu.android.dns.DnsManager;
import com.qiniu.android.dns.Domain;
import com.qiniu.android.dns.IResolver;
import com.qiniu.android.dns.NetworkInfo;
import com.qiniu.android.dns.local.AndroidDnsServer;
import com.qiniu.android.dns.local.Resolver;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dns;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Simon on 6/20/16.
*/
public class HappyDnsTest extends InstrumentationTestCase {
private OkHttpClient client;
@Override
public void setUp() throws Exception {
System.out.println("setUp.....liubin");
IResolver[] resolvers = new IResolver[2];
resolvers[0] = AndroidDnsServer.defaultResolver();
resolvers[1] = new Resolver(InetAddress.getByName("114.114.115.115"));
final DnsManager dnsM = new DnsManager(NetworkInfo.normal, resolvers);
// dnsM.putHosts("training.download.finger66.com", "115.231.75.15");
// Domain d = new Domain("training.download.finger66.com", true, true);
// String[] ips1 = dnsM.query(d);
// System.out.println(1 + ": " + join(ips1, ", "));
//
// String[] ips2 = dnsM.query("training.download.finger66.com");
// System.out.println(2 + ": " + join(ips2, ", "));
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
InetAddress[] ips;
try {
ips = dnsM.queryInetAdress(new Domain(hostname));
} catch (IOException e) {
e.printStackTrace();
throw new UnknownHostException(e.getMessage());
}
if (ips == null) {
throw new UnknownHostException(hostname + " resolve failed");
}
List<InetAddress> l = new ArrayList<>();
Collections.addAll(l, ips);
return l;
}
});
client = builder.build();
}
@SmallTest
public void testReq() {
System.out.println("testReq....liubin");
try {
System.out.println(client.dns().lookup("training.download.finger66.com").toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
client.newCall(new Request.Builder()
.url("https://training.download.finger66.com/qiniu_do_not_delete.gif")
.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response res) throws IOException {
System.out.println(res.toString());
}
});
}
public static String join(Object[] array, String sep) {
return join(array, sep, "");
}
public static String join(Object[] array, String sep, String prefix) {
if (array == null) {
return "";
}
int arraySize = array.length;
if (arraySize == 0) {
return "";
}
if (sep == null) {
sep = "";
}
if (prefix == null) {
prefix = "";
}
StringBuilder buf = new StringBuilder(prefix);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(sep);
}
buf.append(array[i] == null ? "" : array[i]);
}
return buf.toString();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。