码迷,mamicode.com
首页 > Web开发 > 详细

使用webview,给自己看的,别人可能看不懂

时间:2014-12-08 19:45:40      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:使用webview



package com.etron.dacangjia.news;


import java.util.ArrayList;

import java.util.regex.Matcher;

import java.util.regex.Pattern;


import org.json.JSONArray;

import org.json.JSONObject;


import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

import android.annotation.SuppressLint;

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.ActionBar;

import android.view.View;

import android.webkit.WebView;

import android.widget.TextView;

import android.widget.Toast;


import com.etron.dacangjia.R;

import com.etron.dacangjia.lib.Common;

import com.etron.dacangjia.lib.NetManager;

import com.etron.dacangjia.lib.NetManager.NetCallBack;


@SuppressLint("NewApi")

public class NewsDetailActivity extends SwipeBackActivity {

private static final String TAG = "NewsDetailActivity";

private final String encoding = "utf-8";

final String mimeType = "text/html";

private WebView newswebView;

private NewsDetailActivity maActivity;

private TextView tv_name;

private TextView tv_info;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Common.debug(TAG, "NewsActivity onCreate");

setContentView(R.layout.find_news_detail_activity);

ActionBar actionBar = getSupportActionBar();

actionBar.hide();

maActivity = this;

init();

// initNews();

}


@SuppressLint("SetJavaScriptEnabled")

private void init() {

// TODO Auto-generated method stub 第一步:

newswebView = (WebView) findViewById(R.id.newswebView);

newswebView.getSettings().setJavaScriptEnabled(true);

// 显示图片

newswebView.getSettings().setBlockNetworkImage(false);

try {

String url = Common.getNewsDetaiUrl(id);

getNewsDtail cb = new getNewsDtail();

NetManager.shareInstance().GET(url, null, cb);

// newswebView.loadUrl(url);

} catch (Exception e) {

Toast.makeText(this, "没有该网页", Toast.LENGTH_SHORT).show();

}

}


class getNewsDtail implements NetCallBack {


@Override

public void OnError(Object obj, String error) {

// TODO Auto-generated method stub

Common.debug(TAG, "error:" + error.toString());

String regEx = "(\\d+){3}";

Pattern p = Pattern.compile(regEx);

Matcher m = p.matcher(error);

error = m.replaceAll("").trim();

Toast.makeText(maActivity, "" + Common.errorName(maActivity, error), Toast.LENGTH_SHORT).show();

}


@Override

public void OnResult(Object obj, JSONObject ret) {

// TODO Auto-generated method stub

if (ret.optString("status").equals("success")) {

Common.debug(TAG, "status:" + ret.optString("status"));

JSONObject doc = ret.optJSONObject("doc");

setMsg(doc);

}

}

}


@Override

protected void onDestroy() {

Common.debug(TAG, "onCreate method is onDestroy");

super.onDestroy();

}


public void setMsg(JSONObject doc) {

Common.debug(TAG, "doc:" + doc.toString());

String content = doc.optString("content");

//关键第二步:

newswebView.loadDataWithBaseURL("about:blank", content, mimeType, encoding, null);

}

}


使用webview,给自己看的,别人可能看不懂

标签:使用webview

原文地址:http://9105034.blog.51cto.com/9095034/1587619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!