यह स्टैक ओवरफ़्लो पर मेरा पहला प्रश्न है। आम तौर पर मैं अपने आप पर जवाब पा सकता हूं, लेकिन इस बार मुझे कहीं भी एक सभ्य उत्तर नहीं मिल रहा है। मैं एंड्रॉइड के लिए ऐप्स बनाने में भी नया हूं, इसलिए मेरे नोबन्स के लिए अग्रिम में खेद है।http अनुरोध को रद्द करने के लिए कैसे करें
स्थानीय आईपी एड्रेस अनुपलब्ध होने पर x मिलीसेकंड के बाद 'एंड्रॉइड जावा' में http अनुरोध कैसे रद्द कर सकता हूं? मैं AsyncTask का उपयोग कर एक HTML पृष्ठ का अनुरोध कर रहा हूं। कक्षा का कोड नीचे दिया गया है।
मेरे पास अब क्या है, onPreExecute() में परिभाषित एक टाइमर, जो X miliseconds के बाद ऑनकैंकल() को सत्य पर सेट करता है। doInBackground() एक स्ट्रीम इत्यादि खोलता है और उसके बाद यह एक स्ट्रिंग को स्ट्रीम लिखता है।
समस्या यह है कि जब स्थानीय आईपी एड्रेस अनुपलब्ध होता है तो url.openStream() फ़ंक्शन java.net तक चल रहा रहता है। कनेक्ट समय अपवाद समय के कारण में आता है। मुझे नहीं पता कि ऑनकैनल() (यदि यह भी संभव है) का उपयोग करके इस कमांड को कैसे इंटरप्ट करना है।
तो मैं url.openStream कमांड को कैसे इंटरप्ट कर सकता हूं? या सामान्य रूप से AsyncTask थ्रेड को समाप्त करें?
private class htmlToString extends AsyncTask<Void,Void,Void> {
public htmlToString asyncObj;
@Override
protected void onPreExecute(){
asyncObj = this;
new CountDownTimer(connectionTimeout, connectionTimeout) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
// stop async task if not in progress
if (asyncObj.getStatus() == AsyncTask.Status.RUNNING) {
asyncObj.cancel(true); // <- how can i cancel the doInBackground with this ?
Log.i("timer", "...still trying to connect...");
}
}
}.start();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(url_string[button_state]);
final InputStream url_inputstream = url.openStream(); // <- if the local ip adress is unavailable, this command keeps running until ETIMEDOUT kicks in
InputStreamReader url_inputstreamreader = new InputStreamReader(url_inputstream);
BufferedReader in = new BufferedReader(url_inputstreamreader);
String inputLine;
while ((inputLine = in.readLine()) != null)
page = page.concat(inputLine);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void params){
// some code
}
}
Edit1: यह एंड्रॉयड स्टूडियो 1.5.1
EDIT2 में किया जाता है: मैं अपने दम पर इसे हल। मैं सिर्फ जावा द्वारा निर्धारित टाइमआउट को छोटा करता हूं। मैंने इसे doInBackground() में किया है (कोड नीचे दिया गया है)। इसका मतलब यह भी था कि मैं timEreExecute() के अंदर टाइमर को पूरी तरह से स्क्रैप कर सकता था।
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(url_string[button_state]);
URLConnection url_connection = url.openConnection();
url_connection.setConnectTimeout(connectionTimeout); // timout is set here
final InputStream url_inputstream = url_connection.getInputStream();
InputStreamReader url_inputstreamreader = new InputStreamReader(url_inputstream);
BufferedReader in = new BufferedReader(url_inputstreamreader);
String inputLine;
while ((inputLine = in.readLine()) != null)
page = page.concat(inputLine);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
कहाँ मैं इस कोड रख सकता हूं? मुझे निम्न त्रुटि संदेश भी मिलता है: "प्रतीक 'httpclient' को हल नहीं किया जा सकता है"। मैंने इसे टाइमर के "ऑनफिनिश" फ़ंक्शन में डालने का प्रयास किया है। मैंने asclcObj के साथ httpclient को प्रतिस्थापित करने का भी प्रयास किया। इसके अलावा मैंने इसे मुख्य -> ऑनक्रेट() में डालने की कोशिश की और httpclient को HTMLToString क्लास के साथ प्रतिस्थापित किया जिसे मैंने बुलाया, लेकिन फिर मुझे त्रुटि संदेश मिलता है: "प्रतीक को हल नहीं कर सकता 'getConnectionManager'" – Malone