2011-03-25 17 views
7

हाय
एक सूची को देखते हुए के लिए नहीं बुलाया जाता है मैं एक वेब दृश्य जो सर्वर से एक छवि फ़ाइल लोड करना चाहिए है, जब वहाँ कोई छवि वर्तमान मैं एक डमी छवि मैं करने की कोशिश कीएंड्रॉयड WebViewClient onReceivedError एक 404 त्रुटि

की जरूरत
holder.image.setWebViewClient(new WebViewClient() 
{ 
        @Override 
       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
       { 

        System.out.println("description error" + description); 
        view.setVisibility(View.GONE); 

       } 

       @Override 
       public void onPageFinished(WebView view, String url) { 

        view.setVisibility(View.VISIBLE); 


       } 


    }); 

मैं, onPageFinished श्रोता के बाद हर छवि url लोड किया जाता है कहा जाता है एक FrameLayout में एक डमी छवि के साथ इस वेबव्यू है, लेकिन onReceivedError एक यूआरएल जो 404 error.Any उत्पादन अनुमान इसे कैसे करना के लिए नहीं बुलाया जाता है।

+0

यह WebView के साथ नहीं किया जा सकता है, तो आप फिर भी बुनियादी httpclient का उपयोग करें और प्रतिक्रिया कोड के लिए देख सकते हैं। यह कैसे करें इसे करने के लिए यहां एक लिंक दिया गया है: http://stackoverflow.com/questions/2592843/android-how-get-the-status-code-of-an-httpclient-request – Machine

+0

ऐसा लगता है कि यह नहीं किया जा सकता : http://stackoverflow.com/questions/5124052/android-webviewclient-onerrorreceived-not-being-called-when-there-is-a-404 –

+0

मैंने एचटीपी क्लाइंट का उपयोग करने की कोशिश की और एचटीपीस्टैटस की जांच करने पर मैंने यूआरएल लोड किया है HttpStatus रिटर्न त्रुटि संदेश तब मैं यूआरएल लोड करने से रोकता हूं, इसके बजाय एक नो-छवि पीएनजी प्रदर्शित करता हूं। क्या यह विधि एक बोझिल है, क्या कोई इसके लिए एक विकल्प सुझा सकता है। – ganesh

उत्तर

0

वह कोड सही दिखता है; क्या यह संभव है कि आपका पृष्ठ 404 त्रुटि उत्पन्न नहीं कर रहा है?

0
holder.image.setWebViewClient(new WebViewClient() { 

    boolean bReceivedError = false; 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
           String description, String failingUrl) { 
     bReceivedError = true; 
     view.setVisibility(View.GONE); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     if(!bReceivedError) 
     view.setVisibility(View.VISIBLE); 
    } 
    }); 
0

@Neeraj सही रास्ते पर है, लेकिन मेरे एप्लिकेशन का वेब दृश्य की एक ताज़ा की अनुमति देता है, तो मैं किसी भी नए URL लोड से पहले त्रुटि राज्य स्पष्ट करने की जरूरत है। इसके अलावा, त्रुटि ध्वज को माता-पिता गतिविधि पर डेटा सदस्य के रूप में संग्रहीत किया जाना चाहिए ताकि यह PageStart() और onPageFinish() पर जारी रहे - उन विधियों को ऑनरर() के बाद कहा जा सकता है।

public class MyActivity extends Activity { 
    private boolean isError; 
    ... 
    protected void onResume() { 
     super.onResume(); 
     isError = false; 
     myWebView.loadUrl(myUrl); 
    } 

    public class MyWebViewClient extends WebViewClient { 
    /** 
    * can be called even after error (embedded images?), so error flag must keep state as data-member in activity, cleared by activity before each loadUrl();   
    */ 
     @Override 
     public void onPageFinished(WebView view, String url) { 
     if (!isError) 
      showContent(); 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     isError = true; 
     showError(); 
     } 
3

मैं आज एक ही मुद्दा था,

समस्या: onPageFinished हमेशा कहा जाता है। अगर कोई त्रुटि है तो इसे एरर रिसीव के बाद बुलाया जाएगा।

यह समाधान मैंने पाया है: WebViewClient.onReceivedError के बजाय मैं WebViewClient.onReceivedHttpError (ओवरराइड करने के लिए किया था

holder.image.setWebViewClient(new WebViewClient() { 

    private boolean error; 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 

     super.onPageStarted(view, url, favicon); 
     error = false; 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 

     error = true; 
     System.out.println("description error" + description); 
     view.setVisibility(View.GONE); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 

     if (!error) { 
      view.setVisibility(View.VISIBLE); 
     } 
     error = false; 
    } 

}); 
3

)()।

@Override 
    public void onReceivedHttpError(final WebView view, final WebResourceRequest request, WebResourceResponse errorResponse) { 
     final int statusCode; 
     // SDK < 21 does not provide statusCode 
     if (Build.VERSION.SDK_INT < 21) { 
      statusCode = STATUS_CODE_UNKNOWN; 
     } else { 
      statusCode = errorResponse.getStatusCode(); 
     } 

     Log().d(LOG_TAG, "[onReceivedHttpError]" + statusCode); 
    } 

WebClient प्रलेखन से:

/** 
* Notify the host application that an HTTP error has been received from the server while 
* loading a resource. HTTP errors have status codes &gt;= 400. This callback will be called 
* for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to 
* perform minimum required work in this callback. Note that the content of the server 
* response may not be provided within the <b>errorResponse</b> parameter. 
* @param view The WebView that is initiating the callback. 
* @param request The originating request. 
* @param errorResponse Information about the error occured. 
*/ 
public void onReceivedHttpError(
     WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { 
} 
+2

ऑनरिसहेड एचटीपीईआरआरआई उपलब्ध नहीं है अगर एपीआई स्तर <23 – Michael

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