Okhttp

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.get_1).setOnClickListener(this);
        findViewById(R.id.get_2).setOnClickListener(this);
        findViewById(R.id.post_1).setOnClickListener(this);
        findViewById(R.id.post_img).setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_1:
                //get 同步请求
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        OkHttpClient client=new OkHttpClient();
                        Request request=new Request.Builder().url("http://i.jandan.net/?oxwlxojflwblxbsapi=get_recent_posts&include=url,date,tags,author,title,excerpt,comment_count,comment_status,custom_fields&page=1&custom_fields=thumb_c,views&dev=1").build();
                        Call call=client.newCall(request);
                        try {
                            //同步get请求,一定开启子线程,不能再主线程操作。
                            Response response=call.execute();
                            String result=  response.body().string();
                            System.out.println(result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                }.start();
                break;
            case R.id.get_2: {


                //{"msg":"登录成功","code":"0","data":{"age":null,"appkey":"6954cf0a92113517","appsecret":"8B4D8218280B5D848297ABD5606671F6","createtime":"2018-02-23T14:46:59","email":null,"fans":null,"follow":null,"gender":null,"icon":"https:\/\/www.zhaoapi.cn\/images\/1519288811963IMG_20180222_154327.jpg","latitude":null,"longitude":null,"mobile":"13520273561","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","praiseNum":null,"token":"967E3AD75E8C28809E1908B134AAE1FE","uid":12218,"userId":null,"username":"13520273561"}}
                //get异步请求
                OkHttpClient client = new OkHttpClient();
                // Request request=new Request.Builder().url("http://i.jandan.net/?oxwlxojflwblxbsapi=get_recent_posts&include=url,date,tags,author,title,excerpt,comment_count,comment_status,custom_fields&page=1&custom_fields=thumb_c,views&dev=1").build();
                 Request request=new Request.Builder().url("https://www.zhaoapi.cn/user/login?mobile=13520273561&password=123456").build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //网络请求失败以后的调用。
                        System.out.println("failure=="+e.getMessage());
                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                    //  网络请求成功,回调。
                        System.out.println("异步获取数据:::"+response.body().string());
                    }
                });
            }
                break;
            case R.id.post_1:
            {
                //post请求,提交(key-value请求参数)(表单)
                OkHttpClient client=new OkHttpClient();
                RequestBody body=new FormBody.Builder()
                        .add("mobile","13520273561")
                        .add("password","123456").build();
                Request request=new Request.Builder().url("https://www.zhaoapi.cn/user/login").post(body).build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {


                    }


                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        System.out.println("post=="+response.body().string());
                    }
                });
            }
                break;
            case R.id.post_img:
            {
                //token":"967E3AD75E8C28809E1908B134AAE1FE","uid":12218
                String sdpath= Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera";
                File file=new File(sdpath);
                if(file.exists()){
                    File[] files=file.listFiles();
                    if(files!=null&&files.length>0){
                        //相册内的图片文件
                        File imgfile=files[0];
                        OkHttpClient client=new OkHttpClient();
                        //准备requestbody
                        RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpg"),imgfile);


                        RequestBody body=new MultipartBody.Builder()
                                //设置类型
                                .setType(MultipartBody.FORM)
                                //添加文件
                                .addFormDataPart("file","img.jpg",requestBody)
                                //添加其他类型参数
                                .addFormDataPart("token","967E3AD75E8C28809E1908B134AAE1FE")
                                .addFormDataPart("uid","12218")


                                .build();


                        Request request=new Request.Builder().url("https://www.zhaoapi.cn/file/upload").post(body).build();
                        Call call=client.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {


                            }


                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                System.out.println("上传图片=="+response.body().string());
                            }
                        });
                    }
                }
            }
                break;
        }
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值