okhttp3.0的工具类

来源:互联网 发布:安悦溪演技知乎 编辑:程序博客网 时间:2024/06/06 13:38

异步post请求 

 1.1//异步请求post    提交 键值对数据(key_value数据)

public void postAyncHttp(String url,Map<String,String> data) {

      /*  RequestBody formBody = new FormBody.Builder()
                .add("userName", "10")
                .add("loginPwd","2323")

                .build();*/


        FormBody.Builder builder = new FormBody.Builder();
          for (Map.Entry<String,String> item :data.entrySet()){
              builder.add(item.getKey(),item.getValue());
          }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("msg",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.i("wangshu", str);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyApp.getContext(), "请求成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }

        });

    }

1.2异步post请求 提交 json数据

  public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .build();


    public String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .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 {

            }
        });

同步post请求

2..1//同步post请求  (参数)提交json数据(因为是同步请求时放在子线程里)

  public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient client = new OkHttpClient.Builder()
//            .sslSocketFactory(createSSLSocketFactory())
//            .hostnameVerifier(new TrustAllHostnameVerifier())
            .connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .build();


    public String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            return "";
        }
    }
例如这样调用上面的2.1方法:
JSONObject req = new JSONObject();req.put("pageIndex", page);req.put("pageSize", pagesize);String response = OKhttpHelper.getInstance().post(GET_LIVELIST, req.toString());



1 0
原创粉丝点击