1 Star 1 Fork 19

开发团队 / HarmonyHttpClient

forked from 黄海彬 / HarmonyHttpClient 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

HarmonyHttpClient

简介

鸿蒙上使用的Http网络框架,里面包含纯Java实现的HttpNet,类似okhttp使用,支持同步和异步两种请求方式;还有鸿蒙版retrofit,和Android版Retrofit相似的使用,解放双手般优雅使用注解、自动解析json

很遗憾,目前没能直接发布bintray,DevEco Studio上传bintray,gradle安装不通过,所以如果要使用,clone下来,引入module即可

HttpNet使用方式

构建GET请求:和okhttp类似

RequestParams params = new RequestParams()
                .put("userName","oscer")
                .put("pwd","oschina");

Request request = new Request.Builder().encode("UTF-8")
                .method("GET")
                .timeout(13000)
                .url("http://www.oschina.net")
                .build();

构建POST请求:


//请求参数
RequestParams params = new RequestParams()
                .put("userName","oscer")
                .putFile("fileName","file")
                .put("pwd","oschina");
//请求对象
Request request = new Request.Builder()
                .encode("UTF-8")
                .method("POST")
                .params(params)
                .timeout(13000)
                .url("http://www.oschina.net")
                .build();

POST JSON 请求构建:

Request request = new Request.Builder()
                .encode("UTF-8")
                .method("POST")
                .content(new JsonContent("json")
                .timeout(13000)
                .url("http://www.oschina.net")
                .build();

执行请求:


HttpNetClient client = new HttpNetClient();
client.setProxy("192.168.1.1",80);//您也可以开启该客户端全局代理
client.addInterceptor(new Interceptor() {
            @Override
            public void intercept(Request request) {
                Log.e("请求拦截器当前线程: " + Thread.currentThread().getName() + "  --  " + request.url());
            }
        });
//执行异步请求
client.newCall(request)
                //如果采用上传文件方式,可以在这里开启上传进度监控
                .intercept(new InterceptListener() {
                    @Override
                    public void onProgress(final int index, final long currentLength, final long totalLength) {
                        Log.e("当前进度", "  --  " + ((float) currentLength / totalLength) * 100);
                    }
                })
                .execute(new Callback() {
                    @Override
                    public void onResponse(Response response) {
                        String body = response.getBody();
                        InputStream is = response.toStream();//如果采用下载,可以在这里监听下载进度
                    }

                    @Override
                    public void onFailure(Exception e) {
                        Log.e("onFailure", " onFailure " + e.getMessage());
                    }
                });

// 也可以在线程中执行同步请求
try {
     Response response = client.newCall(request).execute();
     String body = response.getBody();
}catch (Exception e){
      e.printStackTrace();
}

Retrofit使用方式,网络实现基于前面的 HttpNetClient


// 构建请求java接口,采用动态代理+注解实现
public interface LoginService {

    //普通POST
    @Headers({"Cookie:cid=adcdefg;"})
    @POST("api/users/login")
    Call<BaseModel<User>> login(@Form("email") String email,
                                @Form("pwd") String pwd,
                                @Form("versionNum") int versionNum,
                                @Form("dataFrom") int dataFrom);

    // 上传文件
    @POST("action/apiv2/user_edit_portrait")
    @Headers("Cookie:xxx=hbbb;")
    Call<String> postAvatar(@File("portrait") String file);


    //JSON POST
    @POST("action/apiv2/user_edit_portrait")
    @Headers("Cookie:xxx=hbbb;")
    Call<String> postJson(@Json String file);

    //PATCH
    @PATCH("mobile/user/{uid}/online")
    Call<ResultBean<String>> handUp(@Path("uid") long uid);
}

执行请求


public static final String API = "http://www.oschina.net/";
public static Retrofit retrofit = new Retrofit();
retrofit.registerApi(API);//注册api
retrofit.setConverterFactory(new ConverterFactory() {
            @Override
            public void convert(com.haibin.retrofit.Response response) {
                response.setBodyString("{json}");//拦截响应数据,修改
                Log.e("响应转换器当前线程: " + Thread.currentThread().getName());
            }
        });
//执行异步请求
retrofit.from(LoginService.class)
         .login("xxx@qq.com", "123456", 2, 2);
         .withHeaders(Headers...)
         .execute(new Callback<BaseModel<User>>() {
                 @Override
                 public void onResponse(Response<BaseModel<User>> response) {

                 }

                 @Override
                 public void onFailure(Exception e) {

                 }
 });

//当然也支持同步请求

Response<BaseModel<User>> response = retrofit.from(LoginService.class)
         .login("xxx@qq.com", "123456", 2, 2);
         .withHeaders(Headers...)
         .execute();
MIT License Copyright (c) 2020 黄海彬 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

鸿蒙上使用的Http网络框架,里面包含纯Java实现的HttpNet,类似okhttp使用,支持同步和异步两种请求方式;还有鸿蒙版retrofit,和Android版Retrofit相似的使用,解放双手般优雅使用注解、自动解析json 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/OAGroup/harmony-http-client.git
git@gitee.com:OAGroup/harmony-http-client.git
OAGroup
harmony-http-client
HarmonyHttpClient
master

搜索帮助

14c37bed 8189591 565d56ea 8189591