OKHttp的Get和Post请求小案例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.atguigu.okhttpsample.MainActivity">

    <Button
        android:id="@+id/btn_get"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="get请求" />
    <Button
        android:id="@+id/btn_post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="post请求" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示请求数据" />

</LinearLayout>

点击事件

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_get = (Button) findViewById(R.id.btn_get);
    btn_post = (Button) findViewById(R.id.btn_post);
    tv_result = (TextView) findViewById(R.id.tv_result);
    //设置点击事件
    btn_get.setOnClickListener(this);
    btn_post.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_get:
            getDataFromByGet();
            break;
        case R.id.btn_post:
            getDataFromByPost();
            break;
    }
}

OKHttp的get请求

private void getDataFromByGet() {

    new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                String resutl = get("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
                Message msg = Message.obtain();
                msg.what = GET;
                msg.obj = resutl;
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}


/**
 * get请求
 * @param url
 * @return
 * @throws IOException
 */
private  String get(String url) throws IOException {
    Request request = new Request.Builder()
            .url(url)
            .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
}

OKHttp的post请求

private void getDataFromByPost() {
    new Thread(){
        @Override
        public void run() {
            super.run();

            String resutl = null;
            try {
                resutl = post("http://api.m.mtime.cn/PageSubArea/TrailerList.api","");
                Message msg = Message.obtain();
                msg.what = POST;
                msg.obj = resutl;
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }.start();
}
/**
 * post请求
 * @param url
 * @param json
 * @return
 * @throws IOException
 */
private  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();
    return response.body().string();
}

在Handler中显示数据

**
 * get请求
 */
private static final int GET = 1;
/**
 * post请求
 */
private static final int POST = 2;
private Button btn_get,btn_post;
private TextView tv_result;
private OkHttpClient client = new OkHttpClient();
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case GET://get请求
                tv_result.setText(msg.obj.toString());
                break;
            case POST://post请求
                tv_result.setText(msg.obj.toString());
                break;
        }
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值