作者:冰weiter | 来源:互联网 | 2023-05-19 16:28
MainActivitypublicclassMainActivityextendsAppCompatActivity{privatestaticfinalS
//MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity---";
//?keywords=笔记本&page=1
private static String goods_url = "https://www.zhaoapi.cn/product/searchProducts";
private XListView xListView;
private int page = 1;
List list = new ArrayList<>();
private MyAdapter myAdapter;
private String keyword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化界面
initViews();
//通过网络获取数据
getDataFromNet();
}
private void getDataFromNet() {
HttpUtils httpUtils = HttpUtils.getInstance();
try {
keyword = URLEncoder.encode("笔记本", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String url = goods_url + "?source=android&keywords=" + keyword + "&page=" + page;
Log.d(TAG, "getDataFromNet: " + url);
httpUtils.get(url);
httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
@Override
public void getSuccess(String json) {
Gson gson = new Gson();
GoodsBean goodsBean = gson.fromJson(json, GoodsBean.class);
List list1 = goodsBean.getData();
//设置适配器
if (page == 1) {
list.clear();
}
list.addAll(list1);
myAdapter.notifyDataSetChanged();
//记得让头布局或者脚布局收回
if (page == 1) {
xListView.stopRefresh();
} else {
xListView.stopLoadMore();
}
}
@Override
public void getError(String error) {
}
});
}
private void initViews() {
xListView = findViewById(R.id.xlistview);
//xListView的使用
xListView.setPullRefreshEnable(true);//设置可以刷新
xListView.setPullLoadEnable(true);//设置可以上拉
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
//当刷新的时候回调
page = 1;
getDataFromNet();
}
@Override
public void onLoadMore() {
//当上拉加载的时候回调
page++;
getDataFromNet();
}
});//设置监听
//先创建适配器,方便以后刷新,注意,不要频繁的new Adapter
myAdapter = new MyAdapter(MainActivity.this, list);
xListView.setAdapter(myAdapter);
}
}
//HttpUtils
public class HttpUtils {
private static final String TAG = "HttpUtils---";
private MyHandler myHandler = new MyHandler();
private static final int SUCCESS = 0;
private static final int ERROR = 1;
//单利
private static HttpUtils httpUtils = new HttpUtils();
private HttpUtilListener httpUtilsListener;
private HttpUtils() {
}//构造方法私有化
public static HttpUtils getInstance() {
if (httpUtils == null) {
httpUtils = new HttpUtils();
}
return httpUtils;
}
//封装get
public void get(final String url) {
new Thread() {
@Override
public void run() {
try {
URL u = new URL(url);
HttpURLConnection cOnnection= (HttpURLConnection) u.openConnection();
connection.setConnectTimeout(3000);
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
String json = inputStream2String(inputStream);
//发送数据
Message message = myHandler.obtainMessage();
message.what = SUCCESS;
message.obj = json;
myHandler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
Message message = myHandler.obtainMessage();
message.what = ERROR;
message.obj = e.getMessage();
myHandler.sendMessage(message);
}
}
}.start();
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
//成功
String json = (String) msg.obj;
Log.d(TAG, "handleMessage: " + json);
httpUtilsListener.getSuccess(json);
break;
case ERROR:
//失败
String error = (String) msg.obj;
Log.d(TAG, "handleMessage: " + error);
httpUtilsListener.getError(error);
break;
}
}
}
//1.定义接口
public interface HttpUtilListener {
void getSuccess(String json);
void getError(String error);
}
//2.外部访问的方法
public void setHttpUtilsListener(HttpUtilListener httpUtilsListener) {
this.httpUtilsListener = httpUtilsListener;
}
public String inputStream2String(InputStream inputStream) {
int len = 0;
byte[] butter = new byte[1024];
StringBuffer stringBuffer = new StringBuffer();
try {
while ((len = inputStream.read(butter)) != -1) {
String s = new String(butter, 0, len);
stringBuffer.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
}