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

时间:2014-08-13 09:58:15

标签: java android apache http httpclient

我重写了DefaultHttpClient,并将我的Cache机制作为RequestInterceptor和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;
        }
    };
}

然后我处理响应:

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);
    }

我的问题在于Galaxy s5。 我得到一个例外:

  

07-29 21:00:57.227 28164 29824 W System.err:java.lang.ClassCastException:org.apache.http.entity.FileEntity无法强制转换为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)

在这里我看到一个方法: org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:880)

我没有发现任何相关信息。 也许三星有它自己的这类课程的实现? 我没有S5在设备上运行它。 为什么我会收到此错误? 感谢

1 个答案:

答案 0 :(得分:0)

我不确定这是否会对您有所帮助,而且我怀疑您在这段时间后仍在寻求帮助,但也许其他人会觉得这很有用。我发现在HTTP特别是Verizon S5上,Apache HTTP看起来并不是非常可靠。尝试在Android应用程序上使用Restlet客户端连接到JSON服务器时遇到此问题。我不断收到本地主机的连接错误,因为我试图连接到远程服务器。

漫长的反复试验使我相信三星正在覆盖或提供自己的Apache HTTP实现,因为使用Restlet内部连接器并且不提供任何其他更改可以解决问题。它似乎与某些软件更新有关,因为出厂重置将允许Apache HTTP在停止工作之前的短时间内工作。看看是否有其他人遇到这个问题会很有趣。