okHttpClient异步同步的get和post请求方法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/thw_m/article/details/78417537
package com.example.lianxiday03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Timeout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


      //  getEnqueue(); //get请求的异步
       // postEnqueue(); //post请求的异步
        getExecute();//get请求的同步
   }

    //get请求的异步
    private void getEnqueue(){

        OkHttpClient client = new OkHttpClient();

        //创建一个request对象
        Request request = new Request.Builder()
                .url("http://120.27.23.105/product/getProducts?pscid=39&page=1")
                .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 {
                System.out.println("getEnqueue = "+response.body().string());
            }
        });
    }

    //post请求异步
    private void postEnqueue(){
        OkHttpClient client = new OkHttpClient();

        //post方式 需要表单提交参数
        RequestBody requestBody = new FormBody.Builder()
                .add("pscid","39")
                .add("page","1")
                .build();

         Request request = new Request.Builder()
                 .url("http://120.27.23.105/product/getProducts")
                 .post(requestBody)
                 .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 {
                //运行在子线程,,
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"1111",Toast.LENGTH_SHORT).show();
                    }
                });

                System.out.println("postEnqueue = "+response.body().string());
            }
        });
    }

    //get请求同步
    private void getExecute(){
        //同步方法.需要开启子线程里运行
    new Thread(new Runnable() {
           @Override
           public void run() {

               try {
                   OkHttpClient client = new OkHttpClient();

                   Request request = new Request.Builder()
                           .url("http://120.27.23.105/product/getProducts?pscid=39&page=1")
                           .build();

                   Call call = client.newCall(request);

                   Response response = call.execute();

                   //拿出来的东西 是字符串
                   String result = response.body().string();

                   System.out.println("getExecute = "+result);

               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }).start();

    }
}
展开阅读全文

没有更多推荐了,返回首页