简体   繁体   中英

org.apache.http.impl.client.DefaultRequestDirector method executeSB (Galaxy S5)

I'm overriding DefaultHttpClient, and pass my Cache mechanism as a RequestInterceptor and ResponseInterceptor

new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params) {

        @Override
        protected HttpRequestExecutor createRequestExecutor(){
            return cache == null ? super.createRequestExecutor() : cache.createRequestExecutor();
        }

        @Override
        protected BasicHttpProcessor createHttpProcessor() {
            BasicHttpProcessor processor = super.createHttpProcessor();
            if(null != cache){
                processor.addRequestInterceptor(cache);
                processor.addResponseInterceptor(cache);
            }
            return processor;
        }

        @Override
        protected HttpContext createHttpContext() {
            // Same as DefaultHttpClient.createHttpContext() minus the
            // cookie store.
            HttpContext context = new BasicHttpContext();
            context.setAttribute(
                    ClientContext.AUTHSCHEME_REGISTRY,
                    getAuthSchemes());
            context.setAttribute(
                    ClientContext.COOKIESPEC_REGISTRY,
                    getCookieSpecs());
            context.setAttribute(
                    ClientContext.CREDS_PROVIDER,
                    getCredentialsProvider());
            return context;
        }
    };
}

Then I process Response:

private void processResponse(String requestLine, CachedItem item, HttpResponse response, HttpContext context){
    ... some code
                switch(response.getStatusLine().getStatusCode()){
                    case HttpStatus.SC_NOT_MODIFIED:
                        Log.v(TAG, "processResponse, not modified");
                        updateResponseWithCachedFile(response);
                        break;
                    case HttpStatus.SC_OK:
                        Log.v(TAG, "processResponse, updating cache");
                        saveEntity(response);
                        break;
                }
     ...some code
    }

public synchronized void updateResponseWithCachedFile(HttpResponse response){
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new FileEntity(new File(mCachedFilePath), "" /* it is internal, not used */));
    }

public synchronized void saveEntity(final HttpResponse response){
        try{
            File cacheFile = new File(mCachedFilePath);
            Utils.delete(TAG + " removing obsolete file", cacheFile);
            if(Logger.assertIfFalse(cacheFile.createNewFile(), TAG, "failed to create file " + cacheFile)){
                Files.copy(new InputSupplier<InputStream>() {
                    @Override
                    public InputStream getInput() throws IOException{
                        return response.getEntity().getContent();
                    }
                }, cacheFile);
            }


        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        updateResponseWithCachedFile(response);
    }

My problem is on the Galaxy s5. I get an Exception:

07-29 21:00:57.227 28164 29824 W System.err: java.lang.ClassCastException: org.apache.http.entity.FileEntity cannot be cast to org.apache.http.entity.BasicHttpEntity 07-29 21:00:57.227 28164 29824 W System.err: at org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:880) 07-29 21:00:57.227 28164 29824 W System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:675) 07-29 21:00:57.227 28164 29824 W System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:567) 07-29 21:00:57.227 28164 29824 W System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:491) 07-29 21:00:57.227 28164 29824 W System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:469) 07-29 21:00:57.227 28164 29824 W System.err: at com.my_application.service.background.HttpRequest$1.run(HttpRequest.java:138) 07-29 21:00:57.227 28164 29824 W System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 07-29 21:00:57.227 28164 29824 W System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 07-29 21:00:57.227 28164 29824 W System.err: at java.lang.Thread.run(Thread.java:841)

and here i see a method: org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:880)

I didn't found anything about this. Maybe Samsung has it's own implementations of this classes? I Don't have S5 to run this on the device. Why do I get this error? Thanks

I am not sure if this will help you and I doubt you are still looking for help after all this time but maybe someone else will find this useful. I have found that Apache HTTP does not seem to be very reliable on S5's specifically Verizon S5's. I encountered this problem while trying to use Restlet clients on an Android application to connect to a JSON server. I continually would receive a connection error to the local host as odd as that was since I was attempting to connect to a remote server.

A long process of trial and error led me to believe that Samsung is overriding or providing their own implementation of Apache HTTP at a low level as using the Restlet internal connector and providing no other changes resolves the issue. It appears to be tied into some software update because a factory reset will allow Apache HTTP to work for a short amount of time before stop working again. It would be interesting to see if anyone else encounters this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM