OkHttp getand post 请求




okHttp   2.0请求方式

package utils;


import android.util.Log;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by lenovo on 2017/1/4.
 * OkHttp的异步请求
 */
public final class YiBuQingQiu {

    private static String json = null;

    public static String GetQingQiu(String url) {

        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                json = response.body().string();
            }
        });
        return json;
    }

    public static String PostQingQiu(String url, String page) {
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //设置参数
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("page", page);
        //创建一个Request
        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();
        //请求加入调度
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                json = response.body().string();
            }
        });
        return json;
    }

}


okHttp   3.0请求方式



      post 请求


String path = "http://v.juhe.cn/weixin/query";
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//获得OkHttpClient 对象
OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .addNetworkInterceptor(interceptor)
        .build();
RequestBody body = new FormBody.Builder().add("key", "").build();

Request request = new Request.Builder().url(path).post(body).build();

client.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();
}});

Get请求



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值