httpclient.execute有效,但重载函数会抛出异常

时间:2013-07-05 11:19:30

标签: java android httpresponse

我正在编写一个Android应用程序。当我使用时,它将HTTPPost发送到服务器并接收答案:

public final HttpResponse execute (HttpUriRequest request)

没关系,

但是当我尝试使用时:

public T execute (HttpUriRequest request, ResponseHandler<? extends T> responseHandler)

它抛出ClientProtocolException

由于某些原因我想使用第二个功能,我该怎么办?什么是例外?

这是使用第一个函数的代码:

  HttpPost httppost = new HttpPost("http://foo.Com/GeneralControls/Service.asmx/Login");
  DefaultHttpClient httpclient = new DefaultHttpClient(); 
  HttpResponse response =  httpclient.execute(httppost) ;

以下是使用第二个函数的代码:

   HttpPost httppost = new HttpPost("http://foo.Com/GeneralControls/Service.asmx/Login");
   DefaultHttpClient httpclient = new DefaultHttpClient(); 
   ResponseHandler<String> responseHandler=new BasicResponseHandler();
   String response = httpclient.execute(httppost , responseHandler) ;

抛出ClientProtocolException。

2 个答案:

答案 0 :(得分:0)

请参阅以下代码对我来说工作正常

        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE,
                Util.cookieStore);
        try {
            HttpResponse response = httpclient.execute(httppost,
                    localContext);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

但是你试图传递ResponseHandler并接受httpContext

答案 1 :(得分:0)

问题是协议问题,Web服务正在更改目标URL,并产生异常