Android reads table data of JSP page

When doing this, I found that I didn't have enough basic knowledge of java, so I learned a lot in this process. For example, private variables and member variables, that is, how to set variables that can be referenced by the entire project in java. In addition to these, I learned how to use the listView control of Android studio and how to grab web data. I feel that I have reviewed the array knowledge I learned before. I learned C,C, and java are still very different. In a word, this small function makes me more confident in facing java.

1, Define 2D array variables 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; }
}

Two, 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"};              //Here is the column name of each column in the ListView display
    int[] to = {R.id.user_name, R.id.user_id};   //Here is the id of the control in the list item that the ListView displays for each column


    String[] userId = {"1001", "1002", "1003", "1004"};  //Here is the ID corresponding to the person name
    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);       //Set main layout for MainActivity
      
        //Create the ArrayList object;
        list = new ArrayList<HashMap<String, String>>();
        T1=(TextView)findViewById(R.id.textView) ;
        //Store the data in the ArrayList object. The structure of data arrangement is that a row of data in the ListView corresponds to a HashMap object,
        //The HashMap object takes the column name as the key, the Value of the column as the Value, adds the information of each column to the map, and then corresponds each column to
        //map object added to ArrayList

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

        for (int i = 0; i < 4; i++) {
            map = new HashMap<String, String>();       //To avoid null pointer exceptions, create several map objects in a few columns
            map.put("id", userId[i]);
            map.put("name",testStr1[i]);
            list.add(map);
        }

        //Create a SimpleAdapter object
        SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, from, to);
        //Call the setListAdapter method of ListActivity to set the adapter for 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;
    }
}

3, 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();
                    //Two-dimensional array
                    testStr22[i][j] = text;
                }
            }

            return testStr22;
        } catch (Exception e) {
            String [][]testStr2={{"Read failed 1","w"},{"Read failed 2","222"},{"Read failed 3","222"},{"Read failed 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 error of Caused by: android.os.NetworkOnMainThreadException is found on the simulator or logcat You can add the following code after the 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

Tags: Android Java JSP simulator

Posted on Fri, 03 Apr 2020 10:23:57 -0700 by GrizzlyBear