主要分为三点补充
1.get和post方式请求访问网络
| 记得一定要处理异常 |
| package com.example.four_content; |
| |
| import java.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.io.OutputStream; |
| import java.net.HttpURLConnection; |
| import java.net.MalformedURLException; |
| import java.net.ProtocolException; |
| import java.net.URL; |
| import java.nio.charset.StandardCharsets; |
| import java.util.Map; |
| |
| public class Get_Post_Activity { |
| public static String get(String path) { |
| String result = null; |
| try { |
| URL url = new URL(path); |
| HttpURLConnection coon = (HttpURLConnection) url.openConnection(); |
| coon.setRequestMethod("GET"); |
| coon.setConnectTimeout(5000); |
| int responseCode = coon.getResponseCode(); |
| if (responseCode == 200) { |
| InputStream is = coon.getInputStream(); |
| |
| BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); |
| StringBuilder stringBuilder = new StringBuilder(); |
| String line; |
| while ((line = reader.readLine()) != null) { |
| |
| stringBuilder.append(line).append(System.lineSeparator()); |
| } |
| result = stringBuilder.toString(); |
| } |
| |
| } catch (ProtocolException e) { |
| throw new RuntimeException(e); |
| } catch (MalformedURLException e) { |
| throw new RuntimeException(e); |
| } catch (IOException e) { |
| throw new RuntimeException(e); |
| } |
| return result; |
| } |
| |
| |
| public static String post(String path, Map<String, Object> map) { |
| String result = null; |
| try { |
| URL url = new URL(path); |
| HttpURLConnection coon = (HttpURLConnection) url.openConnection(); |
| coon.setRequestMethod("POST"); |
| coon.setConnectTimeout(5000); |
| |
| |
| String data = ""; |
| |
| coon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| |
| coon.setRequestProperty("Content-Length", data.length() + ""); |
| coon.setDoOutput(true); |
| |
| OutputStream outputStream = coon.getOutputStream(); |
| outputStream.write(data.getBytes()); |
| |
| int responseCode = coon.getResponseCode(); |
| if (responseCode == 200) { |
| InputStream is = coon.getInputStream(); |
| BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); |
| StringBuilder stringBuilder = new StringBuilder(); |
| String line; |
| while ((line = reader.readLine()) != null) { |
| stringBuilder.append(line).append(System.lineSeparator()); |
| } |
| result = stringBuilder.toString(); |
| } |
| |
| } catch (IOException e) { |
| throw new RuntimeException(e); |
| } |
| return result; |
| } |
| } |
| new Thread() { |
| @Override |
| public void run() { |
| |
| |
| String s = Get_Post_Activity.get("http://本机ip地址:8080/Chapter09/goods/goods_list_data.json"); |
| Log.i("高远", "RUN:"+ s); |
| } |
| } |
2.解析json数据

| 也别忘记处理异常 |
| |
| JSONArray jsonArray = new JSONArray(s); |
| for (int i = 0; i < jsonArray.length(); i++) { |
| |
| JSONObject jsonObj = jsonArray.getJSONObject(i); |
| String id = jsonObj.optString("id"); |
| String count = jsonObj.optString("count"); |
| String goodsName = jsonObj.optString("goodsName"); |
| String goodsPic = jsonObj.optString("goodsPic"); |
| Log.i("高远", "run: " + id + count + goodsName + goodsPic); |
| } |
| |
| |
| Gson gson = new Gson(); |
| Type type = new TypeToken<List<Goods>>() { |
| }.getType(); |
| List<Goods> goods = gson.fromJson(s, type); |
| for (Goods good : goods) { |
| int id = good.getId(); |
| String goodsName = good.getGoodsName(); |
| String count = good.getCount(); |
| String goodsPic = good.getGoodsPic(); |
| Log.i("低近", "run: " + id + count + goodsName + goodsPic); |
| } |
3.对handler的再理解
| 之前那个项目里的MHandle为主线程 所有操作在这个内部完成 子线程向主线程发送消息 |
| class MHandle extends Handler { |
| @Override |
| public void dispatchMessage(@NonNull Message msg) { |
| super.dispatchMessage(msg); |
| if (msg.what == MSG_GOODS_OK) { |
| if (msg.obj != null) { |
| String result = (String) msg.obj; |
| List<Goods> goodsList = getGoodsList(result); |
| pddAdapter.setData(goodsList); |
| } |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?