首页 > 其他分享 >okhttp

okhttp

时间:2022-10-10 17:56:47浏览次数:52  
标签:Request OkHttpClient call file NotNull okhttp new

    public static String upload(String url, File file) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(),
                        RequestBody.create(MediaType.parse("application/octet-stream"), file))
                .build();
        Request request = new Request.Builder().url(url).post(formBody).build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }
//上传pdf文件
//System.out.println(upload("http://182.92.156.167:8081/jeeplus/api/customer/webOssuploads", new File("C:/test.txt")));

 //get请求

OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.get().url("http://a.laremehpe.xyz/php/dictionaryLookUp.php?showRecord=Throne&userName=test").build();
Call call = okHttpClient.newCall(request);

call.enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
System.out.println(response.body().string());
}

@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {

}
});

 

标签:Request,OkHttpClient,call,file,NotNull,okhttp,new
From: https://www.cnblogs.com/laremehpe/p/16776630.html

相关文章

  • HttpClient和OkHttp发送http请求
    根据技术选型总结常见的三种方式发送http请求,本问介绍框架中常用的HttpClient和OkHttp方式,其他两种如下链接​​​springboot中使用restTemplate发送http请求​​ 一、http......
  • 36. 网络加载框架OkHttp的同步与异步请求
    36.网络加载框架OkHttp的同步与异步请求36.1简介官方地址https://github.com/square/okhttpandroid网络框架之OKhttp一个处理网络请求的开源项目,是安卓端最火热......
  • 37. OKhttp自定义配置
    37.OKhttp自定义配置37.1Builder构建者OkHttpClientokHttpClient=newOkHttpClient.Builder().build();提供更多操作37.2拦截器OkHttpClientokHttpClient=newO......
  • android小项目-菜谱APP-首页面设计上(RecyclerView+OKHttp+GSON+Glide)
    在上一节中,已经搭建好了导航栏,现在开始完成首页内容的设计。任务描述:实现从网络HTTP接口获取菜谱数据,并完成菜谱App主页面的布局设计。设计思路:1.数据层面,使用showapi的......
  • OkHttp的使用
    在build.gradle中引用如下代码dependencies{//defineaBOManditsversionimplementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))//def......