implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration);
MyAsynctack asynctack = new MyAsynctack();
asynctack.execute(pach);
private class MyAsynctack extends AsyncTask<String,Void,String> {
//请求网络,加载耗时操作
@Override
protected String doInBackground(String... params) {
String json="";
try {
//将接口转化成url
URL url = new URL(params[0]);
//打开HttpURLConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置请求方式
urlConnection.setRequestMethod("GET");
//设置超时链接
urlConnection.setConnectTimeout(5000);
//设置读取超时
urlConnection.setReadTimeout(5000);
//得到响应码 进行判断
if (urlConnection.getResponseCode()==200){
//得到数据
InputStream inputStream = urlConnection.getInputStream();
//转换
// json = StreamToString(inputStream);
String s = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
//网络加载之前
@Override
protected void onPreExecute() {
super.onPreExecute();
}
//网络加载之后
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析
Gson gson = new Gson();
//将解析出来的数据 存入集合
List<LlationBean.ResultBean.DataBean> list = (List<LlationBean.ResultBean.DataBean>) gson.fromJson(s, LlationBean.class);
// List<LlationBean.ResultBean.DataBean.StepsBean> list2 = (List<LlationBean.ResultBean.DataBean.StepsBean>) gson.fromJson(s, LlationBean.class);
//创建适配器
Log.i("TAG",list.toString());
Mybuters mybuters = new Mybuters(MainActivity.this, list);
listView.setAdapter(mybuters);
}
//网络的加载速度
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
//将字节流转换成字符流的方法
private String StreamToString(InputStream inputStream) {
//字符流
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
//缓冲流
BufferedReader reader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
try {
if ((s=reader.readLine())!=null){
builder.append(s);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}