Day5

HttpClient

image




    HttpEntity entity = response.getEntity();
    String body = EntityUtils.toString(entity);
    System.out.println("服务端返回的数据为:" + body);

    //关闭资源
    response.close();
    httpclient.close();


//创建httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

HttpClient:Apache 提供的 HTTP 客户端库,用于发送 HTTP 请求。

//创建请求对象
//Get请求
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
//Post请求
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");

HttpGet:表示 HTTP GET 请求的对象,包含请求 URL。

HttpPost:表示 HTTP POST 请求的对象,用于发送带实体数据的请求。



//发送请求,接受响应结果
CloseableHttpResponse response = httpclient.execute(httpGet);
//获取服务端返回的状态码
int statusCode = response.getStatusLine().getStatusCode();

CloseableHttpResponse:表示 HTTP 响应,包含状态码、响应头和响应体。

HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);

HttpEntity:表示响应的实体部分(即响应内容)。

EntityUtils:工具类,用于处理 HTTP 实体,例如将实体内容转换为字符串。



// 创建JSON对象,设置请求参数
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "admin");
jsonObject.put("password", "123456");
// 将JSON对象转换为StringEntity,用于设置请求体
StringEntity entity = new StringEntity(jsonObject.toString());
// 设置请求体的编码方式为UTF-8
entity.setContentEncoding("UTF-8");
// 设置请求体的内容类型为application/json
entity.setContentType("application/json");
// 将请求体设置到POST请求中
httpPost.setEntity(entity);

JSONObject:JSON 处理库中的对象(通常来自org.jsoncom.alibaba.fastjson等库),用于构建 JSON 格式的数据。

GET 请求不需要 JSON,是因为它通过 URL 传递简单参数,适合轻量查询;
POST 请求常用 JSON,是因为它通过请求体传递复杂数据,适合提交 / 修改操作,而 JSON 恰好能满足结构化、大容量的需求。



//关闭资源
response.close();
httpclient.close();


微信小程序开发

image

注意事项

  • 调试基础库的版本要选择2.25以下否咋会报错
  • appid中的value填微信小程序开发管理中的AppID
  • secret中的value填微信小程序中申请的AppSecret
  • js_code中的value是代码运行生成的code
  • grant_type中的valueauthorization_code


导入商品浏览功能

注意:视频时没有Day4是因为Day4是作业,再资料里面有Day4的代码都是管理端,没有进行Day4会影响导入商品

如果发现再导入时的update没有报错时,则说明你Day3的资料没看

<update id="update">
        update setmeal
        <set>
            <if test="name != null">name=#{name},</if>
            <if test="categoryId != null">category_id=#{categoryId},</if>
            <if test="price != null">price=#{price},</if>
            <if test="image != null">image=#{image},</if>
            <if test="description != null">description=#{description},</if>
            <if test="status != null">status=#{status},</if>
            <if test="updateTime != null">update_time=#{updateTime},</if>
            <if test="updateUser != null">update_user=#{updateUser},</if>
        </set>
        where id=#{id}
    </update>
posted @ 2025-07-21 16:20  li_hc  阅读(6)  评论(0)    收藏  举报