http客户端的体系结构

时间:2013-10-21 18:25:27

标签: java android

我正在使用http客户端开发应用程序,我想知道一些优雅的问题。 这是标准的java http客户端,它在后台线程中工作,并通过事件传递数据(通过覆盖方法实现)。我有后台请求的特殊类,它实现了方法sendRequest()

protected void sendRequest(final String url)  {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                URI website = null;
                try {
                    website = new URI(url);
                } catch (URISyntaxException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                HttpGet request = new HttpGet();
                request.setURI(website);
                HttpResponse response = null;
                try {
                    response = client.execute(request, httpContext);
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                if (response != null)
                {
                    HttpEntity entity = response.getEntity();
                    try {
                        InputStream is = entity.getContent();
                        if (Debug.isDebuggerConnected()==true)
                        {
                            String data = convertStreamToString(is);
                            int code = response.getStatusLine().getStatusCode();                          
                            if (httpEvent!=null)
                                httpEvent.HttpResponseArrived(data, code);
                        }
                        else
                            httpEvent.HttpResponseArrived(convertStreamToString(is),response.getStatusLine().getStatusCode());
                        }
                    catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        });
        t.start();
    }

还有子类,对于Web服务器的API,有类似的方法:

public void getSomeData(SomeParams param)
    {
        sendRequest("http://xxx.yy"+gson.toJson(param));
        httpEvent = new HttpHandler()
        {
            @Override
            public void HttpResponseArrived(String data, int code)
            {
                switch (code)
                {
                    case 200:
                        //some code
                        break;
                    case 401:
                        //some code
                        break;

                }
            }
        };
    }

我的问题是:处理服务器错误有多优雅,例如401?我需要在一个地方执行此操作,方法是发送请求 - sendRequest()。乍一看它很容易:只需处理401,如果它是因为过期的cookie - 调用方法Login()(在我的设计中,它看起来像getSomeData)。但我希望,不仅仅是再次登录,我需要请求数据,因为错误我没能得到。当然,我可以在每个开关中实现调用Login()方法,如下所示:

case 401:
                    {
                        Login(CurrentContext.AuthData.Login, CurrentContext.AuthData.Password);
                        break;
                    }

但是登录事件在Login()方法中实现; 此外,我可以编写sendRequest(字符串authdata),HttpHandler的subscrube以及实现此代码的递归调用方法。但是我知道,这不是一个很好的决定。 我真的希望,有人已经解决了这个问题,并且有办法,把它变成美丽的代码吧! 谢谢,如果你能读完这篇文章:)

1 个答案:

答案 0 :(得分:1)

回答不评论。 尝试使用http客户端框架 - 我更喜欢Apache HTTPClient。它提供对请求和响应的广泛控制。此外,它支持最常见的方法,如GET POST等。还提供Cookie管理,重定向处理和SSL支持。不要发明已经发明的东西。 HttpClient - use v4.x

相关问题