public class MainActivity extends AppCompatActivity {
private ListView list_view;
private String spec="http://172.17.8.100/mobile/exam/findNewList";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_view = findViewById(R.id.list_view);
jiexi(spec);
}
private void jiexi(String spec) {
MyAsync myAsync = new MyAsync();
myAsync.execute(spec);
}
public class MyAsync extends AsyncTask<String,Integer,String> {
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(spec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if (connection.getResponseCode()==200){
String s=json2String(connection.getInputStream());
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
// Log.i("aaa","onPostExecute: "+s);
Bean bean = gson.fromJson(s, Bean.class);
final List<Bean.ResultBean.DataBean> data = bean.getResult().getData();
list_view.setAdapter(new MyAdapter(MainActivity.this,data));
//点击事件
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, WebActivity.class);
intent.putExtra("pic",data.get(i).getUrl());
startActivity(intent);
}
});
}
//转流方法
private String json2String(InputStream inputStream) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String str=null;
while ((str=reader.readLine())!=null){
builder.append(str);
}
reader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
强调内容
app 页面
//加 build gunle name:包.app
public class App extends Application {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context=this;
initData();
}
private void initData() {
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(options()).build();
ImageLoader.getInstance().init(configuration);
}
private DisplayImageOptions options() {
DisplayImageOptions displayImageOptions= new DisplayImageOptions.Builder()
.cacheOnDisk(true)// 是否开启内存缓存
.cacheInMemory(true)// 是否开启SD卡缓存
.build();
return displayImageOptions;
}
}