本文介绍了改造和放大器; HTTP补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想用改造打个补丁的要求,但现在我不能okhttp添加到我的类路径中。当我尝试打个补丁的请求,我得到了下面的堆栈跟踪。是否有其他办法,我可以使用补丁,而无需使用okhttp?

  java.net.ProtocolException
        在java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:644)
        在retrofit.client.UrlConnectionClient。prepareRequest(UrlConnectionClient.java:50)
        在retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
        在retrofit.RestAdapter $ RestHandler.invokeRequest(RestAdapter.java:358)
        在retrofit.RestAdapter $ RestHandler.access $ 100(RestAdapter.java:264)
        在retrofit.RestAdapter $ RestHandler $ 2.obtainResponse(RestAdapter.java:315)
        在retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
        在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
        在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
        在retrofit.Platform $的Andr​​oid $ 2 $ 1.run(Platform.java:142)
        在java.lang.Thread.run(Thread.java:1019)
10月6日至9日:53:09.349 1809至1897年/**.****.****** D /改造:---- END错误


解决方案

这是 HttpURLConnection类的限制。您可以使用Apache或OkHttp它都支持<$ C C> PATCH 作为替代客户端。这可以明确地在构建器来完成:

  RestAdapter restAdapter =新RestAdapter.Builder()
    .setEndpoint(..)
    .setClient(新OkClient())
    。建立()

此外,一些服务器允许指定 X-HTTP-方法,覆盖头改变的方式。有了这个,你会发出 POST ,但包括 PATCH ,因为这头值。同样,这需要服务器的支持。

最后,另外一个选项是继承改造的 UrlConnectionClient 和使用反射来改变其中包含HTTP方法领域。这是很脆弱的,容易出现破损的未来,而且是在我看来,最坏的选择。

So I want to make a PATCH request using Retrofit but currently I cannot add okhttp to my classpath. When I attempt to make a PATCH request I get the stack trace below. Is there any other way I could use Patch without using okhttp?

    java.net.ProtocolException
        at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:644)
        at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:50)
        at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
        at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
        at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
        at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
        at retrofit.Platform$Android$2$1.run(Platform.java:142)
        at java.lang.Thread.run(Thread.java:1019)
06-09 10:53:09.349    1809-1897/**.****.****** D/Retrofit﹕ ---- END ERROR
解决方案

This is a limitation of HttpUrlConnection. You can either use Apache or OkHttp which both support PATCH as an alternative client. This can be done explicitly in the builder:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint(..)
    .setClient(new OkClient())
    .build()

Additionally, some servers allow specifying an X-HTTP-Method-Override header for changing the method. With this you would send a POST but include PATCH as this header value. Again, this requires server support.

Finally, one other option would be to subclass Retrofit's UrlConnectionClient and use reflection to change the field which holds the HTTP method. This is very fragile, prone to future breakage, and is the worst option in my opinion.

这篇关于改造和放大器; HTTP补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:27