OKHttp

 new Thread(new Runnable() {
            @Override
            public void run() {
//          实例化客户端对象
                OkHttpClient okHttpClient = new OkHttpClient();
//                创建一个客户端请求对象(请求方法GET,请求路径)
                Request request = new Request.Builder().method("GET", null).url("http://apicloud.mob.com/v1/weather/query?key=146d30f8f3b93&city=兰州&province=甘肃").build();
                Call call = okHttpClient.newCall(request);
//
                try {
                    Response response = call.execute();
//                    返回的是json数据
                    final String jsonstring = response.body().string();
                    Log.e("TAG", "run: " + jsonstring);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "请求结果:" + jsonstring, Toast.LENGTH_SHORT).show();
                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }




            }
        }).start();
    }




    private void httppost() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("app", "weather.future")
                        .add("weaid", "1")
                        .add("appkey", "10003")
                        .add("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4")
                        .add("format", "json")
                        .build();
                Request request = new Request.Builder().url("http://api.k780.com:88/").method("POST", requestBody).build();


                Call call = okHttpClient.newCall(request);
                try {
                     Response response = call.execute();
                    final String jsonstring = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "post请求结果"+jsonstring, Toast.LENGTH_SHORT).show();
                        }
                    });




                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值