获取数据并显示在html界面

package com.avicsafety.dangerous_chemicals;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avicsafety.conf.Configuration;
import com.avicsafety.conf.Constants;
import com.avicsafety.interfaces.OnGetInfoListener;
import com.avicsafety.interfaces.OnSearchResultListener;
import com.avicsafety.lib.tools.tools;
import com.avicsafety.model.M_LoginInfo;
import com.avicsafety.model.M_Whp_Sgal;
import com.avicsafety.model.P_Whp_Sgal;
import com.avicsafety.service.Info_AccidentCaseManager;
import com.avicsafety.service.LoginManager;

@ContentView(R.layout.activity_accident_info)
public class AccidentCaseInfoActivity extends BaseActivity {

    private String AcId;
    private Info_AccidentCaseManager accidentmanager;
    private P_Whp_Sgal model;
    private String header;
    private String bodyTemp;
    private String body = "";
    private String footer;

    @Override
    protected void InitializeComponent() {
        super.InitializeComponent();

        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());

        //实现Java和js之间的交互
        webView.getSettings().setJavaScriptEnabled(true);
        //解决界面显示乱码问题
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.loadData("", "text/html", "UTF-8");

    }

    @Override
    protected void InitializeData() {

        // 获取assets文件夹中的html文件
        String[] html = this.getAssetsHtml("html/one.html").split(",,,,");
        // 将html文件分割成三部分分割符为,,,,,header,bodyTemp,footer
        header = html[0];
        bodyTemp = html[1];
        footer = html[2];

        accidentmanager = new Info_AccidentCaseManager();

        //接收上个界面传送过来的数据
        AcId = tools.getParam(oThis);
        RequestParams rp = getParams();

        //实现接口
        accidentmanager.getAccidentInfo(rp, new OnSearchResultListener() {

            @Override
            public void onSuccess(List _list) {

                 //关闭进度条
                progressDialog.dismiss();
                // 获取数据成功
                int i = 0;
                // 循环遍历数组(model要从新设置,获取的数据是不对的)
                for (Object o : _list) {
                    final int index = i;

                    //将遍历的数据类型强制类型转换成创建的Model类型
                    model = (P_Whp_Sgal) o;

                //将获取的html字符串数组和获取的数据拼接
                    body += bodyTemp.replace("{{title}}", model.getTitle())
                            .replace("{{content}}", model.getContent())
                            .replace("{{index}}", i + "");

                    // 动态加载TextView
                    TextView btn = new TextView(oThis);
                    btn.setId(index);
                    btn.setText((index + 1 + "")+"、" + model.getTitle());
                    btn.setTextSize(14);
                    btn.setPadding(0, 0, 0, 10);
                    btn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            //实现抽屉中TextView的点击事件,跳到相应内容置顶
                            jump(index);
                        }
                    });

                    //将控件加载到界面上
                    ll_container.addView(btn);

                    i++;

                }
                // 将字符串数组拼接,加载html数据
                // 获取数据中的图片将其显示

                body = body.replace("/showImg", Constants.SERVER_ADDR
                        + "/showImg");
                try {
                    //将html一文件的形式写入到sd卡中 
                    File file = new File(Configuration.BASE_PATH
                            + File.separator + "temp.html");
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    writeTxtFile(header + body + footer, file);
                    Log.v("", file.getAbsolutePath());
                    // 加载html文件
                    webView.loadUrl("file:///" + file.getAbsolutePath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(String message) {
                tools.showAlertTip(message, oThis);
            }
        });     

        //设置对话框的标题

        progressDialog.setMessage(getString(R.string.down_accident_list));

       //显示对话框
        progressDialog.show();

    }

    @Override
    protected void InitializeEvent() {
    }


    // 设置请求服务器的参数
    private RequestParams getParams() {
        M_LoginInfo loginInfo = LoginManager.getLoginInfo();
        String username = loginInfo.getUserName();
        String password = loginInfo.getPassword();

        RequestParams rp = new RequestParams(Constants.PHONE_ACCIDENT_SEACH);
        rp.setConnectTimeout(60000);
        rp.addQueryStringParameter("userName", username);
        rp.addQueryStringParameter("password", password);
        rp.addQueryStringParameter("classname", "Whp_sgal_nr");
        rp.addQueryStringParameter("sgalId", AcId);
        return rp;
    }

    private String getAssetsHtml(String path) {

        // 获取assets文件夹中html文件,已输入流的方式读取文件
        String txt = "";
        try {
            InputStream is = oThis.getAssets().open(path);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            txt = new String(buffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return txt;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_list:
            openRightLayout();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    // 右边菜单开关事件
    public void openRightLayout() {
        if (drawerLayout.isDrawerOpen(accident_case_right_layout)) {
            drawerLayout.closeDrawer(accident_case_right_layout);
        } else {
            drawerLayout.openDrawer(accident_case_right_layout);
        }
    }

//右边抽屉的跳转事件

    public void jump(int n) {
        openRightLayout();
        webView.loadUrl("javascript:jump(" + n + ")");
    }

    //把文件的源码写到一个文件里
    public static boolean writeTxtFile(String content, File fileName)
            throws Exception {
        boolean flag = false;
        FileOutputStream o = null;
        try {
            o = new FileOutputStream(fileName);
            o.write(content.getBytes("UTF-8"));
            o.close();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @ViewInject(R.id.drawerLayout)
    private DrawerLayout drawerLayout;// 抽屉布局

    @ViewInject(R.id.accident_case_right_layout)
    private RelativeLayout accident_case_right_layout;

    @ViewInject(R.id.webview)
    private WebView webView;

    @ViewInject(R.id.ll_container)
    private LinearLayout ll_container;
}
 

转载于:https://my.oschina.net/u/3213186/blog/821486

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值