2016-02-01 16 views
5

यह स्टैक ओवरफ़्लो पर मेरा पहला प्रश्न है। आम तौर पर मैं अपने आप पर जवाब पा सकता हूं, लेकिन इस बार मुझे कहीं भी एक सभ्य उत्तर नहीं मिल रहा है। मैं एंड्रॉइड के लिए ऐप्स बनाने में भी नया हूं, इसलिए मेरे नोबन्स के लिए अग्रिम में खेद है।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; 
    } 

उत्तर

2

httpclient.getConnectionManager().shutdown();

+0

कहाँ मैं इस कोड रख सकता हूं? मुझे निम्न त्रुटि संदेश भी मिलता है: "प्रतीक 'httpclient' को हल नहीं किया जा सकता है"। मैंने इसे टाइमर के "ऑनफिनिश" फ़ंक्शन में डालने का प्रयास किया है। मैंने asclcObj के साथ httpclient को प्रतिस्थापित करने का भी प्रयास किया। इसके अलावा मैंने इसे मुख्य -> ​​ऑनक्रेट() में डालने की कोशिश की और httpclient को HTMLToString क्लास के साथ प्रतिस्थापित किया जिसे मैंने बुलाया, लेकिन फिर मुझे त्रुटि संदेश मिलता है: "प्रतीक को हल नहीं कर सकता 'getConnectionManager'" – Malone

2

आप अनुरोध

httpget.abort(); 

उदाहरण रद्द के लिए नीचे दिए गए कोड का उल्लेख कर सकते हैं:

import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 



public class ClientAbortMethod { 

    public final static void main(String[] args) throws Exception { 
     CloseableHttpClient httpclient = HttpClients.createDefault(); 
     try { 
      HttpGet httpget = new HttpGet(<URL>); 

      System.out.println("Executing request " + httpget.getURI()); 
      CloseableHttpResponse response = httpclient.execute(httpget); 
      try { 
       System.out.println("----------------------------------------"); 
       System.out.println(response.getStatusLine()); 

       // Call abort on the request object 
       httpget.abort(); 
      } finally { 
       response.close(); 
      } 
     } finally { 
      httpclient.close(); 
     } 
    } 

} 
+0

मुझे निम्न त्रुटियां मिलती हैं : "ClosableHttpClient" प्रतीक को हल नहीं कर सकता है, "httpclients" प्रतीक को हल नहीं कर सकता है, "getUri" प्रतीक को हल नहीं कर सकता है, प्रतीक "getStatusLine" को हल नहीं कर सकता है, प्रतीक "abort" को हल नहीं कर सकता प्रतीक "बंद" – Malone

+0

को हल नहीं कर सकता है जो आप त्रुटि कर सकते हैं .. क्या आप कृपया साझा कर सकते हैं उन त्रुटियों – Musaddique

+0

मैंने बहुत जल्द प्रवेश किया, क्षमा करें; पी लेकिन मैंने इसे – Malone

संबंधित मुद्दे