get,post网络请求,下载,上传

//按钮点击事件
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="get请求"
    android:onClick="getClick"/>
//get请求
public void getClick(View view) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
    Request request = new Request.Builder()
            .url("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12")
            .get()
            .build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {


        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String string = response.body().string();
            Log.e("---get","onResponse"+string);
        }
    });
}
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="post请求"
    android:onClick="postClick"/>

//post请求

public void postClick(View view) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
    FormBody formBody = new FormBody.Builder()
            .add("pageSize","10")
            .add("currentPage","1")
            .build();
    Request request = new Request.Builder()
            .url("http://43.143.146.165:7777/foods/postFoods")
            .post(formBody)
            .build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String string = response.body().string();
            Log.e("post","onResponse"+string);
        }
    });
}
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="下载"
    android:onClick="downClick"/>

//下载

public void downClick(View view) {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
    Request request = new Request.Builder()
            .url("https://cdn.jsdelivr.net/gh/LuckyZmj/imgbed@master/galleries/%E7%82%AB%E9%85%B7%E8%B7%91%E8%BD%A6/01.jpg")
            .get()
            .build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            InputStream inputStream = response.body().byteStream();
            byte[] buffer = new byte[1024];
            int len=0;
            FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/car3.jpg");
            while ((len=inputStream.read(buffer))!=-1){
                fileOutputStream.write(buffer,0,len);
            }
        }
    });
}
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="上传"
    android:onClick="upClick"/>

//上传

public void upLoadClick(View view) {
    //创建客户端
    OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
    //创建表单
    MultipartBody multipartBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file","car2.jpg", RequestBody.create(MediaType.parse("media/jpg"),new File("/sdcard/car2.jpg")))
            .build();
    //设置请求
    Request request = new Request.Builder()
            .url("http://10.161.6.10/car/")
            .post(multipartBody)
            .build();
    //响应
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.e("----ssss","onResponse"+"上传成功");
        }
    });
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值