关闭

okhttp3.0的工具类

57人阅读 评论(0) 收藏 举报

异步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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1345次
    • 积分:346
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条