उन लोगों के लिए के लिए चला जाता कह रही है कि इस सवाल का जवाब @ kuester2000 का काम नहीं करता है, कृपया ध्यान रखें कि HTTP अनुरोध, पहले DNS अनुरोध के साथ होस्ट आईपी को खोजने का प्रयास करें और फिर सर्वर पर वास्तविक HTTP अनुरोध करता है, इसलिए आपको DNS अनुरोध के लिए टाइमआउट सेट करने की भी आवश्यकता हो सकती है।
यदि आपका कोड DNS अनुरोध के लिए टाइमआउट के बिना काम करता है तो ऐसा इसलिए होता है क्योंकि आप किसी DNS सर्वर तक पहुंचने में सक्षम होते हैं या आप एंड्रॉइड DNS कैश को मार रहे हैं। जिस तरह से आप डिवाइस को पुनरारंभ करके इस कैश को साफ़ कर सकते हैं।
//Our objective
String sURL = "http://www.google.com/";
int DNSTimeout = 1000;
int HTTPTimeout = 2000;
//Get the IP of the Host
URL url= null;
try {
url = ResolveHostIP(sURL,DNSTimeout);
} catch (MalformedURLException e) {
Log.d("INFO",e.getMessage());
}
if(url==null){
//the DNS lookup timed out or failed.
}
//Build the request parameters
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, HTTPTimeout);
HttpConnectionParams.setSoTimeout(params, HTTPTimeout);
DefaultHttpClient client = new DefaultHttpClient(params);
HttpResponse httpResponse;
String text;
try {
//Execute the request (here it blocks the execution until finished or a timeout)
httpResponse = client.execute(new HttpGet(url.toString()));
} catch (IOException e) {
//If you hit this probably the connection timed out
Log.d("INFO",e.getMessage());
}
//If you get here everything went OK so check response code, body or whatever
प्रयुक्त विधि:
//Run the DNS lookup manually to be able to time it out.
public static URL ResolveHostIP (String sURL, int timeout) throws MalformedURLException {
URL url= new URL(sURL);
//Resolve the host IP on a new thread
DNSResolver dnsRes = new DNSResolver(url.getHost());
Thread t = new Thread(dnsRes);
t.start();
//Join the thread for some time
try {
t.join(timeout);
} catch (InterruptedException e) {
Log.d("DEBUG", "DNS lookup interrupted");
return null;
}
//get the IP of the host
InetAddress inetAddr = dnsRes.get();
if(inetAddr==null) {
Log.d("DEBUG", "DNS timed out.");
return null;
}
//rebuild the URL with the IP and return it
Log.d("DEBUG", "DNS solved.");
return new URL(url.getProtocol(),inetAddr.getHostAddress(),url.getPort(),url.getFile());
}
इस वर्ग this blog post से है
इस कोड को एक कस्टम समय समाप्ति के साथ एक पुस्तिका DNS लुकअप शामिल करने के लिए मूल जवाब फैली हुई है। जाओ और अगर आप इसका इस्तेमाल करेंगे तो टिप्पणियों की जांच करें।
public static class DNSResolver implements Runnable {
private String domain;
private InetAddress inetAddr;
public DNSResolver(String domain) {
this.domain = domain;
}
public void run() {
try {
InetAddress addr = InetAddress.getByName(domain);
set(addr);
} catch (UnknownHostException e) {
}
}
public synchronized void set(InetAddress inetAddr) {
this.inetAddr = inetAddr;
}
public synchronized InetAddress get() {
return inetAddr;
}
}
स्रोत
2015-07-26 23:54:31
क्या होगा यदि आप 'AndroidHttpClient' का उपयोग कर रहे हैं? आप पैरामीटर कैसे संलग्न करेंगे? –
@ थॉमस: मैंने आपके उत्तर को आपके उपयोगकेस – kuester2000
के समाधान के साथ संपादित किया है यदि कनेक्शन समय समाप्त होने पर HttpResponse वापस क्या होगा? एक बार मेरा HTTP अनुरोध करने के बाद, मैं कॉल रिटर्निंग पर स्टेटस कोड की जांच करता हूं, हालांकि कॉल को समय समाप्त होने पर इस कोड को चेक करते समय मुझे NullPointerException मिलता है ... मूल रूप से, कॉल करते समय मैं स्थिति को कैसे संभाला टाइमआउट करता है? (मैं आपके उत्तर में बहुत ही समान कोड का उपयोग कर रहा हूं) – Tim