首页 > 代码库 > 使用webview,给自己看的,别人可能看不懂
使用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 one rror(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,给自己看的,别人可能看不懂