在异步okhttp请求回调函数中更新listview(Update listview in asynchronous okhttp request callback function)

个人理解okhttp同ajax访问逻辑相似,正常情况下,安卓更新ui界面是在主线程中的,在回调函数中是不允许更新ui的,采用如下代码即可实现更新ui

下面字体放大部分,开辟新线程

new Thread(new Runnable() {
                 @Override
                 public void run() {
                     System.out.println("执行了okhttp");
                     FormBody formBody = new FormBody.Builder()
                             .add("area",area)
                             .add("building",place)
                             .add("week",week)
                             .add("xq",week_day)
                             .add("num", String.valueOf(new Date().getTime())).build();
                     //服务器地址String url="http://82.157.106.56:8009/vtServlet";
                     Request request = new Request.Builder().url("http://:8009/vtServlet")
                             .post(formBody).build();
                     Call call = okHttpClient.newCall(request);
                     try {
                         Response response = call.execute();
                         result = response.body().string();
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
                                 //更改UI;
                                 new Handler().postDelayed(new Runnable() {
                                     @Override
                                     public void run() {
                                         istadapter(parseJSONWithJSONObject(result));
                                     }
                                 },1 *1);  
                             }
                         });
                         System.out.println(result);
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }
             }
             ).start();
         }
————————

I understand that okhttp is similar to Ajax access logic. Under normal circumstances, the Android UI update interface is in the main thread, and the UI update is not allowed in the callback function. The following code can be used to update the UI

Enlarge the font below to open up a new thread

new Thread(new Runnable() {
                 @Override
                 public void run() {
                     System.out.println("执行了okhttp");
                     FormBody formBody = new FormBody.Builder()
                             .add("area",area)
                             .add("building",place)
                             .add("week",week)
                             .add("xq",week_day)
                             .add("num", String.valueOf(new Date().getTime())).build();
                     //服务器地址String url="http://82.157.106.56:8009/vtServlet";
                     Request request = new Request.Builder().url("http://:8009/vtServlet")
                             .post(formBody).build();
                     Call call = okHttpClient.newCall(request);
                     try {
                         Response response = call.execute();
                         result = response.body().string();
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
                                 //更改UI;
                                 new Handler().postDelayed(new Runnable() {
                                     @Override
                                     public void run() {
                                         istadapter(parseJSONWithJSONObject(result));
                                     }
                                 },1 *1);  
                             }
                         });
                         System.out.println(result);
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }
             }
             ).start();
         }