论坛元老
- 威望
- 3627
- 贡献
- 3823
- 热心值
- 3
- 金币
- 4612
- 注册时间
- 2020-2-11
|
public class MainActivity extends AppCompatActivity {
private static final int WHAT_SUCCESS = 1;
private static final int WHAT_FAIL = 2;
private ListView lv_main;
private LinearLayout ll_main_loading;
private List<ShopInfo> data;
private ShopInfoAdapter adapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case WHAT_SUCCESS:
ll_main_loading.setVisibility(View.GONE);
// 显示列表
lv_main.setAdapter(adapter);
break;
case WHAT_FAIL:
ll_main_loading.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "加载数据失败!", (int) 1).show();
break;
default:
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = findViewById(R.id.lv_main);
ll_main_loading = findViewById(R.id.ll_main_loading);
adapter = new ShopInfoAdapter();
// 1.主线程,显示提示视图
ll_main_loading.setVisibility(View.VISIBLE);
// 2.分线程,联网请求
// 启动分线程请求服务器动态加载数据并显示
new Thread(() -> {
try {
String jsonStr = requestJson();
System.out.println(jsonStr);
Gson gson = new Gson();
Log.e("gson:", gson + "");
data = gson.fromJson(jsonStr, new TypeToken<List<ShopInfo>>() {
}.getType());
Log.e("json:", data + "");
// 3.主线程,更新界面
// 发送请求成功的消息
handler.sendEmptyMessage(WHAT_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
// 发送请求失败的消息
handler.sendEmptyMessage(WHAT_FAIL);
}
}).start();
}
/**
* 联网请求得到json字符串
*
* @return
*/
private String requestJson() throws Exception {
String result = "";
// 192.168.124.4
// 192.168.0.103
String path = "http://192.168.124.4:9999/vod/gets";
// 1.得到连接对象
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//(4)设置连接超时,读取数据超时
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
//(5)连接服务器
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
//(6)发请求,得到响应数据
InputStream is = connection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.close();
is.close();
result = byteArrayOutputStream.toString();
// (7)断开连接
connection.disconnect();
} else {
// 也可以抛出异常
}
return result;
}
class ShopInfoAdapter extends BaseAdapter {
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
}
// 得到当前行的数据对象
ShopInfo shopInfo = data.get(position);
// 得到当前行的子view
TextView name = convertView.findViewById(R.id.tv_item_name);
TextView id = convertView.findViewById(R.id.tv_item_content);
ImageView imageView = convertView.findViewById(R.id.iv_item_icon);
// 设置数据显示
name.setText(shopInfo.getVideoName());
id.setText(shopInfo.getId());
// 根据图片路径启动分线程动态请求服务加载图片并显示
return convertView;
}
}
}
|
|