httphandler
public class DocumentDetailsActivity extends BaseActivity { public static final String EXTRA_DOC = "doc"; public static final String EXTRA_TITLE = "title"; public static final String EXTRA_URL = "url"; public static final String EXTRA_HIDE_HEADER = "hide_header"; public static final String EXTRA_SHOW_HUD = "show_hud"; private Document doc; private WebView webView; private HttpHandler bookmarkHandler; private HttpHandler unbookmarkHandler; private HttpHandler commentsHandler; private HttpHandler bookmarkStateHandler; private UMSocialService mController; private TextView commentsButton; private ImageView bookmarkButton; private boolean bookmarked; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_document_details); View backIcon = findViewById(R.id.backIcon); backIcon.setVisibility(View.VISIBLE); commentsButton = (TextView) findViewById(R.id.button_comments); bookmarkButton = (ImageView) findViewById(R.id.bookmark); boolean shouldHideHeader = getIntent().getBooleanExtra(EXTRA_HIDE_HEADER, false); if (shouldHideHeader) { View header = findViewById(R.id.header); header.setVisibility(View.GONE); } webView = (WebView) findViewById(R.id.webView); WebSettings setting = webView.getSettings(); setting.setUseWideViewPort(true); setting.setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { // Activities and WebViews measure progress with different scales. // The progress meter will automatically disappear when we reach 100% setProgress(progress * 1000); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); if (getIntent().getBooleanExtra(EXTRA_SHOW_HUD, false)) { if (!DocumentDetailsActivity.this.isFinishing()) { progressDialog = ProgressDialog.show(DocumentDetailsActivity.this, null, "正在加载数据,请稍候……"); progressDialog.setCancelable(true); } } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (progressDialog != null) { progressDialog.dismiss(); progressDialog = null; } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(DocumentDetailsActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } }); loadContent(); commentsHandler = new HttpHandler(this) { @Override public void handleOk(String result) { try { Response rsp = Response.fromCommentsJson(result); if (rsp.isSuccessful()) { commentsButton.setText(String.valueOf(rsp.getTotal())); } } catch (JSONException e) { e.printStackTrace(); UIUtils.showError(DocumentDetailsActivity.this, e); } } }; updateCommentsCount(); bookmarkStateHandler = new HttpHandler(this) { @Override public void handleOk(String result) { try { JSONObject o = new JSONObject(result); if (o.getBoolean("success")) { boolean bookmarked = o.getBoolean("has"); setBookmarked(bookmarked); } } catch(Exception e) { e.printStackTrace(); UIUtils.showError(DocumentDetailsActivity.this, e); } } }; setupBookmarkState(); bookmarkHandler = new BookmarkHandler(this); unbookmarkHandler = new UnBookmarkHandler(this); setupSocialMedia(); } private void loadContent() { doc = (Document) getIntent().getSerializableExtra(EXTRA_DOC); String url = ""; if (doc != null) { url = doc.getMobileUrl(); } else { url = getIntent().getStringExtra(EXTRA_URL); } webView.loadUrl(url); } private void setupSocialMedia() { // 首先在您的Activity中添加如下成员变量 mController = UMServiceFactory.getUMSocialService("com.umeng.share"); mController.getConfig().removePlatform(SHARE_MEDIA.EMAIL, SHARE_MEDIA.SMS, SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN, /*SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,*/ SHARE_MEDIA.TENCENT); mController.getConfig().setPlatformOrder(SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.SINA, SHARE_MEDIA.QZONE, SHARE_MEDIA.QQ); // 添加微信平台 UMWXHandler wxHandler = new UMWXHandler(this, Constants.WECHAT_APPID, Constants.WECHAT_APP_SECRET); wxHandler.addToSocialSDK(); // 支持微信朋友圈 UMWXHandler wxCircleHandler = new UMWXHandler(this, Constants.WECHAT_APPID, Constants.WECHAT_APP_SECRET); wxCircleHandler.setToCircle(true); wxCircleHandler.addToSocialSDK(); //参数1为当前Activity, 参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, Constants.QQ_APPID, Constants.QQ_APPKEY); qqSsoHandler.addToSocialSDK(); //参数1为当前Activity, 参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, Constants.QQ_APPID, Constants.QQ_APPKEY); qZoneSsoHandler.addToSocialSDK(); String content = getString(R.string.app_name); String title = doc.getTitle(); String url = doc.getMobileUrl(); //设置微信好友分享内容 WeiXinShareContent weixinContent = new WeiXinShareContent(); //设置分享文字 weixinContent.setShareContent(content); //设置title weixinContent.setTitle(title); //设置分享内容跳转URL weixinContent.setTargetUrl(url); //设置分享图片 UMImage shareImage; if (doc.getPicUrl() != null) { shareImage = new UMImage(this, doc.getPicUrl()); } else { shareImage = new UMImage(this, R.drawable.ic_launcher); } weixinContent.setShareImage(shareImage); mController.setShareMedia(weixinContent); //设置微信朋友圈分享内容 CircleShareContent circleMedia = new CircleShareContent(); circleMedia.setShareContent(title); //设置朋友圈title circleMedia.setTitle(title); circleMedia.setTargetUrl(url); circleMedia.setShareImage(shareImage); mController.setShareMedia(circleMedia); QQShareContent qqShareContent = new QQShareContent(); //设置分享文字 qqShareContent.setShareContent(content); //设置分享title qqShareContent.setTitle(title); //设置分享图片 qqShareContent.setShareImage(shareImage); //设置点击分享内容的跳转链接 qqShareContent.setTargetUrl(url); mController.setShareMedia(qqShareContent); QZoneShareContent qzone = new QZoneShareContent(); //设置分享文字 qzone.setShareContent(content); //设置点击消息的跳转URL qzone.setTargetUrl(url); //设置分享内容的标题 qzone.setTitle(title); //设置分享图片 qzone.setShareImage(shareImage); mController.setShareMedia(qzone); mController.setShareContent(title); } @Override protected void onStart() { super.onStart(); } /* @Override protected void onPause() { //clear web content to stop video and release resources webView.loadUrl("about:blank"); super.onPause(); } */ @Override public void onBackPressed() { /*if (webView.canGoBack()) { webView.goBack(); return; } */ super.onBackPressed(); } // @Override // public boolean onKeyDown(int keyCode, KeyEvent event) { // if (keyCode == KeyEvent.KEYCODE_BACK) { // if (progressDialog != null) { // progressDialog.dismiss(); // progressDialog = null; // return true; // } // } // return super.onKeyDown(keyCode, event); // } public void share(View view) { // mController.openShare(this, false); mController.openShare(this, new SocializeListeners.SnsPostListener() { @Override public void onStart() { } @Override public void onComplete(SHARE_MEDIA share_media, int eCode, SocializeEntity socializeEntity) { if (eCode == 200) { Toast.makeText(DocumentDetailsActivity.this, "分享成功。", Toast.LENGTH_SHORT).show(); } else { String eMsg = ""; if (eCode == -101){ eMsg = "没有授权。"; } Toast.makeText(DocumentDetailsActivity.this, "分享失败[" + eCode + "] " + eMsg,Toast.LENGTH_SHORT).show(); } } }); } public void onBackImagePressed(View view) { Log.d("debug", "hhhhh"); finish(); } private void updateCommentsCount() { new Thread() { public void run() { HashMap<String, Object> params = new HashMap<String, Object>(); params.put("docId", doc.getDocId()); params.put("currentpage", 1); params.put("pagesize", 20); HttpUtils.post(Constants.kUrlApiGetComments, params, commentsHandler); } }.start(); } public void comments(View view) { Intent intent = new Intent(this, CommentsActivity.class); intent.putExtra(CommentsActivity.EXTRA_DOC, doc); intent.putExtra(CommentsActivity.EXTRA_TITLE, "评论:" + doc.getTitle()); startActivity(intent); } private void setupBookmarkState() { final String username = UserStorage.getUsername(this); if (username != null) { new Thread() { public void run() { HashMap<String, Object> params = new HashMap<String, Object>(); params.put("docId", doc.getDocId()); params.put("userName", username); HttpUtils.post(Constants.kUrlApiIsBookmarked, params, bookmarkStateHandler); } }.start(); } } public void bookmark(View view) { final String username = UserStorage.getUsername(this); if (username == null) { Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); } else { new Thread() { public void run() { if (!bookmarked) { HashMap<String, Object> params = new HashMap<String, Object>(); params.put("siteId", doc.getSiteId()); params.put("channelId", doc.getChannelId()); params.put("docId", doc.getDocId()); params.put("docUrl", doc.getPcUrl()); params.put("docTitle", doc.getTitle()); params.put("userName", username); HttpUtils.post(Constants.kUrlApiBookmark, params, bookmarkHandler); } else { HashMap<String, Object> params = new HashMap<String, Object>(); params.put("docId", doc.getDocId()); params.put("userName", username); HttpUtils.post(Constants.kUrlApiUnbookmark, params, unbookmarkHandler); } } }.start(); } } public void changeFontSize(View view) { WebSettings webSettings = webView.getSettings(); TextSize textSize = webSettings.getTextSize(); if (textSize == WebSettings.TextSize.NORMAL) { textSize = WebSettings.TextSize.LARGER; } else if (textSize == WebSettings.TextSize.LARGER) { textSize = WebSettings.TextSize.SMALLER; } else if (textSize == WebSettings.TextSize.SMALLER) { textSize = WebSettings.TextSize.NORMAL; } webSettings.setTextSize(textSize); } public boolean isBookmarked() { return bookmarked; } public void setBookmarked(boolean bookmarked) { this.bookmarked = bookmarked; bookmarkButton.setImageResource(bookmarked ? R.drawable.star_active : R.drawable.star); } public void goBack(View view) { finish(); } private static class BookmarkHandler extends HttpHandler { private WeakReference<DocumentDetailsActivity> activityWeakReference; public BookmarkHandler(DocumentDetailsActivity activity) { super(activity); activityWeakReference = new WeakReference<DocumentDetailsActivity>(activity); } @Override public void handleOk(String result) { DocumentDetailsActivity activity = activityWeakReference.get(); if (activity != null) { try { JSONObject o = new JSONObject(result); if (o.getBoolean("success")) { activity.setBookmarked(true); UIUtils.showMessage(activity, "已收藏。"); } } catch (Exception e) { e.printStackTrace(); UIUtils.showError(activity, e); } } } } private static class UnBookmarkHandler extends HttpHandler { private WeakReference<DocumentDetailsActivity> activityWeakReference; public UnBookmarkHandler(DocumentDetailsActivity activity) { super(activity); activityWeakReference = new WeakReference<DocumentDetailsActivity>(activity); } @Override public void handleOk(String result) { DocumentDetailsActivity activity = activityWeakReference.get(); if (activity != null) { try { JSONObject o = new JSONObject(result); if (o.getBoolean("success")) { activity.setBookmarked(false); UIUtils.showMessage(activity, "已取消收藏。"); } } catch (Exception e) { e.printStackTrace(); UIUtils.showError(activity, e); } } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。