OkHttpUtils

OkHttpUtils工具类

package com.example.eggplant_doctor.util;

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

import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/14.
 */
public class Okutil {

    private static OkHttpClient client;

    public static String getGet(String url) {

//创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
//new call
        Call call = mOkHttpClient.newCall(request);
//请求加入调度
        try {
            Response response = call.execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    public static String getGetGbk(String url) {

//创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
//new call
        Call call = mOkHttpClient.newCall(request);
//请求加入调度
        try {
            Response response = call.execute();
            return new String(response.body().bytes(),"gbk");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    public static String getPost(String url, Map<String, String> data) throws IOException {
        client = new OkHttpClient();
        FormEncodingBuilder formBuilder = new FormEncodingBuilder();
        for (Map.Entry<String, String> item : data.entrySet()) {
            formBuilder.add(item.getKey(), item.getValue());
           /* Log.i("key", item.getKey());
            Log.i("values", item.getValue());*/
        }

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

        Response response = excute(request);

        if (response.isSuccessful()) {

            return response.body().string();

        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    private static Response excute(Request request) throws IOException {
        return client.newCall(request).execute();
    }


}

在类中调用

 new Thread(new Runnable() {
            @Override
            public void run() {
                String get = Okutil.getGet("http://www.hengboit.com/json/CourseInformation.json");
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson g=new Gson();

                    }
                });

            }
        }).start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值