public class Main2Activity extends AppCompatActivity {
private ListView listView;
private List<ResultData.DataBean> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = findViewById(R.id.lv);
MyTask myTask=new MyTask();
myTask.execute("http://120.27.23.105/product/getProducts?pscid=1&page=1&sort=0");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
intent.putExtra("url",data.get(i).getDetailUrl());//detailUrl字段
startActivity(intent);
}
});
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
//1.创建HttpClient对象
HttpClient httpClient=new DefaultHttpClient();
//2.根据请求方式 创建不同的请求对象
HttpGet httpGet=new HttpGet(strings[0]);
//3.执行
try {
HttpResponse response = httpClient.execute(httpGet);
//4.判断响应码
if(response.getStatusLine().getStatusCode()==200){
//5.得到结果
HttpEntity entity = response.getEntity();
//6.转换并返回
String s = EntityUtils.toString(entity);
return s;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
ResultData resultData = gson.fromJson(s, ResultData.class);
data = resultData.getData();
MyAdapter adapter=new MyAdapter(Main2Activity.this, data);
listView.setAdapter(adapter);
}