okhttp请求网络数据

/**
* 发送网络请求
* @param url 网络地址
* @param username 用户名
* @param password 密码
* @param callback 请求返回
*/
包:implementation ‘com.squareup.okhttp3:okhttp:3.12.0’

protected void sendRequest(String url,String username,String password,Callback callback){
        RequestBody requestBody=new FormBody.Builder().add("city",username)
                .add("appKey",password)
                .build();
        Request request=new Request.Builder().url(url).post(requestBody).build();
        OkHttpClient okHttpClient=new OkHttpClient();
        okHttpClient.newCall(request).enqueue(callback);
    }
private void getrequest(){
    OkHttpClient client=new OkHttpClient();
    final Request request=new Request.Builder().get().url("http://www.baidu.com").build();
    Call call=client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String str=response.body().string();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv.setText(str);
                }
            });
        }
    });
}

private void getimg(){
    OkHttpClient client=new OkHttpClient();
    final Request request=new Request.Builder().get().url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542603500099&di=05ca140c2f6ce7c73076ef3debb98bf0&imgtype=0&src=http%3A%2F%2Fwww.17qq.com%2Fimg_qqtouxiang%2F75142881.jpeg").build();
    Call call=client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            InputStream inputStream=response.body().byteStream();
            final Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    img.setImageBitmap(bitmap);
                }
            });
        }
    });
}

private void postrequest(String name,String pass){
    OkHttpClient client=new OkHttpClient();
    RequestBody formBody=new FormBody.Builder()
            .add("username",name)
            .add("password",pass)
            .build();
    Request request=new Request.Builder().post(formBody).url("http://115.28.211.9:8080/exame/reg_register").build();
    Call call=client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String str=response.body().string();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv.setText(str);
                    Toast.makeText(MainActivity.this,"post请求成功",Toast.LENGTH_SHORT).show();
                }
            });
        }
    });
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值