Detailed tutorial of Android crawling web page JSON data [novice]

Keywords: Android JSON Java Gradle

In recent days, we have done an Android task to get Json. We found that Jsoup can only get static pages, but not dynamic pages.

Some students corrected it for me: Jsoup can crawl the dynamic page, but it must add the request header (data). The following is the correct example:

String url = "Your web interface";
Map data = Maps.newHashMap();
data.put("countryCode", "+86");
data.put("mobile", mobile);
data.put("password", "f20332f07cdaab9ec0472655a83438d4");
data.put("invitionCode", "PU9HFMNMZ2FL");
data.put("validateCode", codeCallable);
data.put("geetest_challenge", challenge);
data.put("geetest_validate", validate);
data.put("geetest_seccode", validate + "|jordan");
String json = JSON.toJSONString(data);
Connection.Response response = Jsoup.connect(url).ignoreContentType(true).data(data).requestBody(json)
.header("Accept", "application/json, text/plain, */*")
.header("Content-Type", "application/json;charset=UTF-8")
.method(Connection.Method.POST).timeout(70000).execute();
System.out.println(response.body());

 

I changed my way of thinking. I used the Alibaba alibaba:fastjson third party package to get json data, which can be directly converted into entity class,

Just a few lines of code. The efficiency of code writing is very high, so I have to praise Ma dada for being a bull.

 

First, we find build.gradle (model e: APP) under Gradle Scripts and add this line of code under build.gradle

    implementation 'com.alibaba:fastjson:1.2.10'

Import the third-party library.

 

Crawled pages: https://www.6tennis.com/schedule

The way to access the third-party library is very simple. First, open the web page you want to access, open your Google browser, and press F12 to enter the developer debugging mode

Then refresh the webpage and it will display some files sniffed by the webpage. Just click the small magnifying glass and input the data you need to search (it can be the data or file name in JSON)

 

Click the third step above, and you will find a series of JSON data in the right-hand box.

{"code":"200","message":"\u8bf7\u6c42\u6210\u529f","data":{"total":69,"list":[{"id":8,"game_name":"ATP Cup","is_grandslm":0,"game_level":"750","ground_name":"\u786c

\u5730","match_time":"01.03-01.12"},{"id":13,"game_name":"\u5361\u5854\u5c14\u516c\u5f00\u8d5b","is_grandslm":0,"game_level":"250","ground_name":"\u786c

\u5730","match_time":"01.06-01.11"},{"id":9,"game_name":"\u963f\u5fb7\u83b1\u5fb7\u56fd\u9645\u8d5b","is_grandslm":0,"game_level":"250","ground_name":"\u786c

\u5730","match_time":"01.12-01.18"},{"id":15,"game_name":"\u5965\u514b\u5170\u559c\u529b\u516c\u5f00\u8d5b","is_grandslm":0,"game_level":"250","ground_name":"\u786c

\u5730","match_time":"01.13-01.18"},{"id":16,"game_name":"\u6fb3\u5927\u5229\u4e9a\u7f51\u7403\u516c

\u5f00\u8d5b","is_grandslm":1,"game_level":"2000","ground_name":"\u786c\u5730","match_time":"01.20-02.03"}]}}

Now let's clear our mind,

1, First of all, we need to access the web page from android through get or post through code, and get the content of the web page

2, After getting the content of the web page, we need to analyze the JSON data and convert the format to our corresponding JSON format

3, After getting JSON, we transform our corresponding entity class and store it in the Arraylist entity table.

4, Let's input the Json data in the Arraylist entity table to different textviews in the foreground, and then add the TextView to the foreground UI

The above ideas are basically implemented in the doInBackground and getView methods of the following code MainActivity.class

 

Why use entity class in the three steps?

The novice should be confused. I believe that all the friends who learn java should know that entity class is an abstract concept.

Let's take a chestnut for example: let's suppose that the basketball shape: round length: 30cm; color: Brown,

Then the basketball here is the entity, the shape, length and color are the attributes, and the circle, 30cm and Brown are the corresponding attribute values of the basketball

Then what is entity class? Let's go back to the above example. Basketball is entity, and basketball is basketball,

Having three attributes of basketball doesn't necessarily belong to basketball, because it may be football.

But basketball must be basketball. Interesting philosophical concepts.

 

Let's take a look at the entity class code of JSON data:

News.class

public class News {
    private int id;//number
    private String game_name;//Event name
    private int is_grandslm;
    private String game_level;//Event level
    private String ground_name;//Place name
    private String match_time;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGame_name() {
        return game_name;
    }

    public void setGame_name(String game_name) {
        this.game_name = game_name;
    }

    public int getIs_grandslm() {
        return is_grandslm;
    }

    public void setIs_grandslm(int is_grandslm) {
        this.is_grandslm = is_grandslm;
    }

    public String getGame_level() {
        return game_level;
    }

    public void setGame_level(String game_level) {
        this.game_level = game_level;
    }

    public String getGround_name() {
        return ground_name;
    }

    public void setGround_name(String ground_name) {
        this.ground_name = ground_name;
    }

    public String getMatch_time() {
        return match_time;
    }

    public void setMatch_time(String match_time) {
        this.match_time = match_time;
    }
}

After careful observation, the content of a JSON data corresponds to the attributes of the entity class one by one.

{"id":8,"game_name":"ATP Cup","is_grandslm":0,"game_level":"750","ground_name":"\u786c

\u5730","match_time":"01.03-01.12"}

In addition, entity classes generally need to have get and set methods, which is convenient to obtain or transfer the property values of entity classes.

 

The code has comments, directly on the code,

MainActivity.java implementation code:

package com.example.tennisdatas;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView Lv_main_list;
    private List<News> NewsList = new ArrayList<News>();
    private ProgressDialog progressDialog;
    private MyAdapter myAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Lv_main_list =(ListView) findViewById(R.id.Lv_main_list);
        //Instantiate progress bar dialog box
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Pro, in the process of life loading!");
        ////Instantiate adapter
        myAdapter = new MyAdapter();
        Lv_main_list.setAdapter(myAdapter);
    }



    //Adapter
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return NewsList.size();
        }

        @Override
        public Object getItem(int i) {
            return NewsList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        //Add the TextView corresponding to JSON data and display it to the main interface, not the main code
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LinearLayout layout = new LinearLayout(MainActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            TextView textViewID = new TextView(MainActivity.this);
            textViewID.setText(NewsList.get(i).getId()+"\t"+" ");
            TextView textViewGameName = new TextView(MainActivity.this);
            textViewGameName.setText(NewsList.get(i).getGame_name()+"\t");
            TextView textViewIs_grandslm= new TextView(MainActivity.this);
            textViewIs_grandslm.setText(NewsList.get(i).getIs_grandslm()+"\t");
            TextView textViewGame_level = new TextView(MainActivity.this);
            textViewGame_level.setText(NewsList.get(i).getGame_level()+"\t");
            TextView textViewGround_name = new TextView(MainActivity.this);
            textViewGround_name.setText(NewsList.get(i).getGround_name()+"\t");
            TextView textViewMatch_time= new TextView(MainActivity.this);
            textViewMatch_time.setText(NewsList.get(i).getMatch_time()+"\t");
            layout.addView(textViewID);
            layout.addView(textViewGameName);
            layout.addView(textViewIs_grandslm);
            layout.addView(textViewGame_level);
            layout.addView(textViewGround_name);
            layout.addView(textViewMatch_time);

            return layout;
        }
    }


    //Get xml data
    public void getJson(View view){
        new MyTask().execute();
    }
    //Get data through asynchronous task class
    class MyTask extends AsyncTask {
        private  News jsonTeacherEntity;
        //Prepare for execution
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {
            //First of all, we need to access the web page from android through get or post through code, and get the content of the web page
            //path is the interface to get JSON data
            String path="https://www.6tennis.com/api/getScheduleList";
            try {
                URL url = new URL(path);
                //Get connection object
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                //Set request method
                httpURLConnection.setRequestMethod("POST");
                //Set connection timeout
                httpURLConnection.setConnectTimeout(5000);
                //Get response code
                int code = httpURLConnection.getResponseCode();

                if (code==200)
                {
                    //The response is successful. Get the data returned by the server
                    final InputStream is = httpURLConnection.getInputStream();
                    //test data
                    StringBuffer stringBuffer = new StringBuffer();
                    String str=null;
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    while ((str=br.readLine())!=null){
                        stringBuffer.append(str);

                    }

                    //Using fastJson to parse Json
                    Log.e("Json data", "Json data: "+stringBuffer.toString() );
                    /* 
                        Because there are some properties in JSON data that we don't need, we need to
                       The required Json data is extracted. Here I write more complex and convenient for learning
                                */
                    String NewsData = stringBuffer.toString();
                    JSONObject jb = JSON.parseObject(NewsData);
                    String Newsdata2 = jb.getString("data");
                    JSONObject jb1 = JSON.parseObject(Newsdata2);
                    String Newsdata3 = jb1.getString("list");
                    Log.e("Newsdata3", "Newsdata3"+Newsdata3);
                    List<News> DNews = JSON.parseArray(Newsdata3, News.class);

                    for (News TT:DNews)
                    {
                        jsonTeacherEntity = new News();
                        int id = TT.getId();
                        String game_name = TT.getGame_name();
                        int is_grandslm = TT.getIs_grandslm();
                        String game_level = TT.getGame_level();
                        String ground_name = TT.getGround_name();
                        String match_time = TT.getMatch_time();
                        jsonTeacherEntity.setId(id);
                        jsonTeacherEntity.setGame_name(game_name);
                        jsonTeacherEntity.setGame_level(game_level);
                        jsonTeacherEntity.setIs_grandslm(is_grandslm);
                        jsonTeacherEntity.setGround_name(ground_name);
                        jsonTeacherEntity.setMatch_time(match_time);
                        NewsList.add(jsonTeacherEntity);
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            //Notify adapter of changes
            myAdapter.notifyDataSetChanged();
            //Cancel progress bar dialog box
            progressDialog.cancel();
        }

    }

}

UI code of main interface: activity main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Refresh"
        android:gravity="center"
        android:layout_gravity="center"
        android:onClick="getJson"
        />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        android:id="@+id/Lv_main_list"
        >
    </ListView>
</LinearLayout>

The code can be copied and debugged directly.

 

Running screenshot:

Author running environment: android studio 3.5.3

 

Android's pit is too big. If you want to jump in, you have to read more Baidu, read more code, and digest all the code to make progress.  

Posted by colinexl on Wed, 22 Apr 2020 21:57:11 -0700