2 jin1091455269 jin1091455269 于 2017.09.06 16:28 提问

取到数据,但第一次不显示,第二次才显示,怎么解决呢?

private OkHttpClient client;
private TextView tvOk;
private Response response;
public String responseOk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    invit();

}

private void invit() {
    tvOk = (TextView) findViewById(R.id.tv_ok);
}


public void btOk(View v) throws IOException {
    new Thread(new Runnable() {
       //因为网络请求是耗时操作,所以要开启一个子线程,放在子线程中请求。

        @Override
        public void run() {

// LogUtil.debug(getClass(), "btOk::::::::::::::::::");
Log.i("数据::::::::::","zz");
// TODO 在线程中请求网络
client = new OkHttpClient();
String url = "http://www.weather.com.cn/adat/cityinfo/101190404.html";//请求设置
Request request = new Request.Builder().url(url).build();
try {

                response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    // 返回数据
                   responseOk = response.body().string();
                    Log.i("网络返回数据::::::::::",responseOk);

// LogUtil.debug(getClass(), "网络返回数据:::::::::::"+responseOk);
}

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }).start();

    tvOk.setText(responseOk+"sdsdds");
        }
Csdn user default icon
上传中...
上传图片
插入图片