Android Development - get Json data and display pictures

Process introduction

  1. Using okhttp network framework for get request to get json data

     //A static method of encapsulated tool class
     public static void sendOkHttpRequest(final String address, final okhttp3.Callback callback) {
         OkHttpClient client = new OkHttpClient();
         CacheControl control =new CacheControl.Builder().build();
         Request request = new Request.Builder()
                 .cacheControl(control)
                 .url(address)
                 .build();
         client.newCall(request).enqueue(callback);
    
     }

    Then we call this method to visit the website and get the returned data

     HttpUtil.sendOkHttpRequest("http://www.baidu.com" + limit, new Callback() {
         @Override
         public void onFailure(Call call, IOException e) {
             Toast.makeText(MainActivity.this, "network error", Toast.LENGTH_SHORT).show();
         }
         @Override
         public void onResponse(Call call, Response response) throws IOException {
             //Get the returned data (according to my example, visit Baidu, the returned data is actually the content of html file
             //If it is another website, it returns the data type returned by its definition.)
             String result = response.body().string();
             //Call GSON framework to parse json data, and return a List of related classes after processing
             List<Data.ResBean> mlist = HttpUtil.parseJSONWithGSON(result);
             //Then process or call the returned data
             mlist.get(1).getXXX();//Calls to related properties
         }
     });
  2. Use GSONFormat plug-in to abstract json data into entity class (plug-in generates automatically)

    Search for the GSONFormat plug-in in Android Studio. After the installation and restart, write a class, press alt+Ins directly, select GSONFormat, and then enter json data to get a corresponding entity class

  3. Use GSON framework to parse json data and get entity class

    The following methods can be written according to your own needs

      /**
      * Calling GSON to parse json data
      * @param jsonData json data
      * @return Return picture entity class list
      */
     public static List<Data.ResBean> parseJSONWithGSON(String jsonData) {
         //json obtained by lightweight Gson parsing
         Gson gson = new Gson();
         Data data = gson.fromJson(jsonData, Data.class);
         List<Data.ResBean> mlist = data.getRes();
         return mlist;
     }
  4. Call the required properties

    Object can get related properties by calling get method. You can call whatever you need. That's not enough.

  5. Use Glide and other picture frames to load network pictures

     Glide.with(context).load(url).into(imageView);

    I used another Ion display picture frame, because there were some bugs in Glide before. It can be loaded for the first time, but it can't be displayed after refreshing the data. Maybe it's because I used Glide version 3.0, and then I thought that Glide version 4.0 was a little muddled, so I chose Ion, which was similar to Glide. The bug before was also solved, so I didn't think too much about it

          Ion.with(holder.imageView.getContext())
                     .load(url)
                     .withBitmap()
                     .placeholder(R.drawable.grey)
                     .intoImageView(holder.imageView);

Tags: Android JSON network OkHttp

Posted on Sun, 01 Dec 2019 11:20:03 -0800 by kingbeastie