{"id":16909112,"url":"https://github.com/linkinstars/http-helper","last_synced_at":"2026-03-15T03:01:05.712Z","repository":{"id":57726230,"uuid":"149101688","full_name":"LinkinStars/http-helper","owner":"LinkinStars","description":"OkHttp封装","archived":false,"fork":false,"pushed_at":"2018-11-07T11:12:55.000Z","size":61,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T11:11:10.898Z","etag":null,"topics":["http","httputils"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LinkinStars.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-17T09:31:52.000Z","updated_at":"2021-06-10T03:18:41.000Z","dependencies_parsed_at":"2022-09-17T13:51:23.139Z","dependency_job_id":null,"html_url":"https://github.com/LinkinStars/http-helper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinkinStars%2Fhttp-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinkinStars%2Fhttp-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinkinStars%2Fhttp-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinkinStars%2Fhttp-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LinkinStars","download_url":"https://codeload.github.com/LinkinStars/http-helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248600332,"owners_count":21131462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","httputils"],"created_at":"2024-10-13T18:54:29.677Z","updated_at":"2026-03-15T03:01:00.664Z","avatar_url":"https://github.com/LinkinStars.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OKHttp封装\n\n## 目标\n调用方法传入url和一个类型，请求后，直接将‘返回的数据’映射到‘该类型的对象’中，并返回相应的请求状态\n\n## 封装内容\n封装OkHttp + fastjson为：\n1. get请求  \n2. post请求 json格式  \n3. post请求 form格式  \n要求请求返回格式都是为json格式   \n\n\n## 使用和更新说明\n**测试案例都在test包下**\n\u003cbr\u003e\u003cbr\u003e\n\nVersion 1.1.0\n更新说明：\n1. 在ResponseAndStatus添加isSuccess方法，用于判断当前请求是否成功。\n2. 添加带有TypeReference请求的方法，解决泛型嵌套的返回类型的优化。  \n\n````\n针对更新中第2点具体说明：  \n我们在实际使用中发现，一般收到的请求都是需要进行泛型嵌套的如：  \n{\"code\":1,\"data\":{\"val\":\"测试\",\"name\":\"名称\",\"id\":1},\"message\":\"请求成功\"}  \n我们需要一个类包含code，message，data字段，其中data字段去用泛型约束。  \n如test包下CommonResponse和SpecificData\n所以我们在调用第三方请求的时候，想传入的类型会有泛型，原来1.0.0的版本只能传class，有些鸡肋\n我们利用TypeReference解决这个问题。\n\n改动之后测试案例如下：\n\n ResponseAndStatus\u003cCommonResponse\u003cSpecificData\u003e\u003e responseAndStatus = HttpHelper.getJson(getUrl, new TypeReference\u003cCommonResponse\u003cSpecificData\u003e\u003e(){});\n \n 最后第三方返回的json会映射到CommonResponse\u003cSpecificData\u003e里面并且泛型里面的类也会有相应的数据。\n        \n````\n\n\u003cbr\u003e\u003cbr\u003e\n\nVersion 1.0.0  \n\n1、get请求  \n````\n    ResponseAndStatus\u003cTestObject\u003e responseAndStatus = HttpHelper.getJson(getUrl, TestObject.class);  \n    getUrl：请求地址  \n    TestObject： 返回数据对象（自定义）返回的json数据会直接映射成对象中的属性，如果直接使用JSONObejct接收自己进行解析也可以\n    responseAndStatus：包含请求状态，当状态为SUCCESS时请求成功，responseData为返回数据，当状态不为SUCCESS则responseData也会有请求数据，用于日志记录\n````\n\n2、post请求json  \n````\n    ResponseAndStatus\u003cTestObject\u003e responseAndStatus = HttpHelper.postJson(postUrl, TestObject.class, requesObject);\n    requesObject：请求对象，该对象为任意对象，会直接转换成json数据发送\n    其余参数与get一致  \n````\n\n3、post请求form  \n````\n    ResponseAndStatus\u003cTestObject\u003e responseAndStatus = HttpHelper.postForm(postUrl, TestObject.class, requestForm);  \n    requestForm：类型是map，其中key-value为表单中对应的数据  \n    其余参数与get一致 \n````\n\n## 推送到本地仓库\n执行build.gradle中的uploadArchives任务就可以推送到本地的Nexus仓库中进行使用，需要修改对应仓库地址。\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkinstars%2Fhttp-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinkinstars%2Fhttp-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinkinstars%2Fhttp-helper/lists"}