From 6c182f3bba1f1dbd6aed6475106fc1ffb24b74a6 Mon Sep 17 00:00:00 2001 From: seven332 Date: Wed, 28 May 2014 23:28:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ex=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hippo/ehviewer/ehclient/EhClient.java | 30 ++++++ .../hippo/ehviewer/network/HttpHelper.java | 91 +++++++++++++++---- 2 files changed, 104 insertions(+), 17 deletions(-) diff --git a/src/com/hippo/ehviewer/ehclient/EhClient.java b/src/com/hippo/ehviewer/ehclient/EhClient.java index cd0ab5d7c..c39ae0b25 100644 --- a/src/com/hippo/ehviewer/ehclient/EhClient.java +++ b/src/com/hippo/ehviewer/ehclient/EhClient.java @@ -447,6 +447,36 @@ public class EhClient { }); } + // Test Ex + + public interface OnTestExListener { + public void onSuccess(); + public void onFailure(String eMsg); + } + + public void testEx(final OnTestExListener listener) { + new Thread(new Runnable() { + @Override + public void run() { + HttpHelper hp = new HttpHelper(mAppContext); + hp.setOnRespondListener(new HttpHelper.OnRespondListener() { + @Override + public void onSuccess(String pageContext) { + if (pageContext.equals("image/gif")) + listener.onFailure("Not login"); + else + listener.onSuccess(); + } + @Override + public void onFailure(String eMsg) { + listener.onFailure(eMsg); + } + }); + hp.contentType(EX_HEADER); + } + }).start(); + } + // Logout private class LogoutPackage { public boolean ok; diff --git a/src/com/hippo/ehviewer/network/HttpHelper.java b/src/com/hippo/ehviewer/network/HttpHelper.java index 3ad957198..8d1c3bff3 100644 --- a/src/com/hippo/ehviewer/network/HttpHelper.java +++ b/src/com/hippo/ehviewer/network/HttpHelper.java @@ -116,31 +116,35 @@ public class HttpHelper { * @return */ public String getEMsg() { - if (mException == null) - return mContext.getString(R.string.em_unknown_error); + return getEMsg(mContext, mException); + } + + public static String getEMsg(Context c, Exception e) { + if (e == null) + return c.getString(R.string.em_unknown_error); - else if (mException instanceof MalformedURLException) - return mContext.getString(R.string.em_url_format_error); + else if (e instanceof MalformedURLException) + return c.getString(R.string.em_url_format_error); - else if (mException instanceof ConnectTimeoutException || - mException instanceof SocketTimeoutException) - return mContext.getString(R.string.em_timeout); + else if (e instanceof ConnectTimeoutException || + e instanceof SocketTimeoutException) + return c.getString(R.string.em_timeout); - else if (mException instanceof UnknownHostException) - return mContext.getString(R.string.em_unknown_host); + else if (e instanceof UnknownHostException) + return c.getString(R.string.em_unknown_host); - else if (mException instanceof ResponseCodeException) - return String.format(mContext.getString(R.string.em_unexpected_response_code), - ((ResponseCodeException)mException).getResponseCode()); + else if (e instanceof ResponseCodeException) + return String.format(c.getString(R.string.em_unexpected_response_code), + ((ResponseCodeException)e).getResponseCode()); - else if (mException instanceof RedirectionException) - return mContext.getString(R.string.em_redirection_error); + else if (e instanceof RedirectionException) + return c.getString(R.string.em_redirection_error); - else if (mException instanceof SocketException) - return "SocketException : " + mException.getMessage(); + else if (e instanceof SocketException) + return "SocketException : " + e.getMessage(); else - return mException.getMessage(); + return e.getMessage(); } private String getPageContext(HttpURLConnection conn) @@ -255,6 +259,59 @@ public class HttpHelper { return null; } + public String contentType(String urlStr) { + mException = null; + + int redirectionCount = 0; + URL url = null; + HttpURLConnection conn = null; + Message msg = null; + if (mListener != null) + msg = new Message(); + try { + + Log.d(TAG, "Http head " + urlStr); + + url = new URL(urlStr); + while (redirectionCount++ < Constant.MAX_REDIRECTS) { + conn = (HttpURLConnection) url.openConnection(); + conn.setInstanceFollowRedirects(true); + conn.connect(); + + String contentType = conn.getHeaderField("Content-Type"); + contentType = contentType == null ? "" : contentType; + if (msg != null) { + Package p = new Package(); + p.listener = mListener; + p.str = contentType; + msg.obj = p; + msg.what = Constants.TRUE; + mHandler.sendMessage(msg); + } + return contentType; + } + } catch (Exception e) { + mException = e; + e.printStackTrace(); + + if (msg != null) { + Package p = new Package(); + p.listener = mListener; + p.str = getEMsg(); + msg.obj = p; + msg.what = Constants.FALSE; + mHandler.sendMessage(msg); + } + return null; + } finally { + if (conn != null) + conn.disconnect(); + } + + mException = new RedirectionException(); + return null; + } + public String post(String urlStr, String[][] args) { mException = null;