參考視頻:
https://www.bilibili.com/video/BV1TJ411v75g?p=6
用到的后臺:https://github.com/TrillGates/SOBAndroidMiniWeb
下載成壓縮包,然后啟動jar包就行
bean類時使用gsonFormat自動生成的一個
代碼:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
GetTextItemAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
RecyclerView recyclerView = findViewById(R.id.result_list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.top = 5;
outRect.bottom = 5;
}
});
mAdapter = new GetTextItemAdapter();
recyclerView.setAdapter(mAdapter);
}
public void loadJson(View view) {
//實(shí)際項目不能這么寫
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://10.0.2.2:9102/get/text");
//URL url = new URL("https://api.sunofbeach.net/shop/api/discovery/categories");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String json;
while ((json = bf.readLine())!=null)
{
response.append(json);
}
//Log.d(TAG, ""+line);
Gson gson = new Gson();
GetTextItem getTextItem = gson.fromJson(response.toString(),GetTextItem.class);
upDateUi(getTextItem);
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
private void upDateUi(final GetTextItem getTextItem) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.setData(getTextItem);
}
});
}
}
public class GetTextItemAdapter extends RecyclerView.Adapter<GetTextItemAdapter.ViewHolder> {
private List<GetTextItem.DataBean> data = new ArrayList<>();
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_get_text, parent, false);
return new ViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
View view = holder.itemView;
TextView title = view.findViewById(R.id.item_title);
GetTextItem.DataBean dataBean = data.get(position);
title.setText(dataBean.getTitle());
ImageView imageView = view.findViewById(R.id.item_img);
Glide.with(view.getContext()).load("http://10.0.2.2:9102"+dataBean.getCover()).into(imageView);
}
@Override
public int getItemCount() {
return data.size();
}
public void setData(GetTextItem getTextItem) {
data.clear();
data.addAll(getTextItem.getData());
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}