代码拉取完成,页面将自动刷新
同步操作将从 黄海彬/HarmonyHttpClient 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
鸿蒙上使用的Http网络框架,里面包含纯Java实现的HttpNet,类似okhttp使用,支持同步和异步两种请求方式;还有鸿蒙版retrofit,和Android版Retrofit相似的使用,解放双手般优雅使用注解、自动解析json
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();
//请求参数
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();
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();
}
// 构建请求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();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。