Android read table data of JSP page

When I did this, I found that I didn't have enough basic knowledge of java, so I learned a lot in this process. Such as private variables, member variables, that is, how java sets variables that can be referenced by the entire project. In addition to these, I also learned how to use the listView control of Android studio and how to capture web page data. I feel that I have revisited the array knowledge I learned before. I used to learn C, C#, and there are still many differences with java. In short, this small function makes me more confident in the face of java.

1. Define a two-dimensional array variable that can be called by the entire project

public class Common {

     private static String[][] testStr2;

    public static String[][] getTestStr2()
        {return testStr2;}

    public    void setTestStr2(String[][] b)
       { this.testStr2=b; }
}

二、Showview.class

import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;

public class Showview extends ListActivity {
    String[] from = {"name", "id"};              //这里是ListView显示内容每一列的列名
    int[] to = {R.id.user_name, R.id.user_id};   //这里是ListView显示每一列对应的list_item中控件的id


    String[] userId = {"1001", "1002", "1003", "1004"};  //这里是人名对应的ID
    static String[] testStr1;
    ArrayList<HashMap<String, String>> list = null;
    HashMap<String, String> map = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listmain);       //为MainActivity设置主布局
      
        //创建ArrayList对象;
        list = new ArrayList<HashMap<String, String>>();
        T1=(TextView)findViewById(R.id.textView) ;
        //将数据存放进ArrayList对象中,数据安排的结构是,ListView的一行数据对应一个HashMap对象,
        //HashMap对象,以列名作为键,以该列的值作为Value,将各列信息添加进map中,然后再把每一列对应
        //的map对象添加到ArrayList中

        Showmain.main(null);
        testStr1=ToOne(Common.getTestStr2());

        for (int i = 0; i < 4; i++) {
            map = new HashMap<String, String>();       //为避免产生空指针异常,有几列就创建几个map对象
            map.put("id", userId[i]);
            map.put("name",testStr1[i]);
            list.add(map);
        }

        //创建一个SimpleAdapter对象
        SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, from, to);
        //调用ListActivity的setListAdapter方法,为ListView设置适配器
        setListAdapter(adapter);
    }



    public static String[] ToOne(String[][] a2) {
        int a = a2.length;
        String a1[];
        a1 = new String[a];
        for (int i = 0; i < a2.length; i++) {
            a1[i] = a2[i][0];
        }
        return a1;
    }
}

三、Showmain.class

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Showmain {
    public static String[][] Str(){
        try {
            Document doc = null;
            doc = Jsoup.connect("http://********.jsp").get();
            Elements trs = doc.select("table").select("tr");
            Elements td11 = trs.get(0).select("td");
            String[][] testStr22=new String[trs.size()][td11.size()];
            for (int i = 0; i < trs.size(); i++) {
                Elements tds = trs.get(i).select("td");
                for (int j = 0; j < tds.size(); j++) {
                    String text = tds.get(j).text();
                    //二维数组
                    testStr22[i][j] = text;
                }
            }

            return testStr22;
        } catch (Exception e) {
            String [][]testStr2={{"读取失败1","w"},{"读取失败2","222"},{"读取失败3","222"},{"读取失败4","222"}};
            e.printStackTrace();
            return  testStr2;

        }
    }

    public static void main(String[] args) {
        String[][] testStr22;
        testStr22= Str();
        int a=testStr22.length;
        int c=testStr22[0].length;
        Common m=new Common();
        m.setTestStr2(testStr22);

        for(int i=0;i<a;i++)
            for (int j = 0; j <c; j++) {
               // Common.testStr2=new String[a][c];
               //Common.testStr2[i][j]=testStr22[i][j];
                System.out.print( Common.getTestStr2()[i][j]+"\n");
            }
    }


}

[Note]: If the Caused by: android.os.NetworkOnMainThreadException error is found on the emulator or logcat, you can add the following code after setContentView(R.layout.activity_listmain) of Showview.class

 if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

Details: http://stackoverflow.com/questions/13136539/caused-by-android-os-networkonmainthreadexception

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325952263&siteId=291194637