使用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);
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。