封装post和get方法

日志拦截器依赖

implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'  

package com.example.mydayokhttp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydayokhttp.utils.OkHttpUtils;

import java.io.IOException;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button Send_Btn;
    private TextView Tv;
    private String mUrl = "http://www.zhaoapi.cn/home/getHome";
    private String mPostUrl = "http://www.zhaoapi.cn/user/login";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        Send_Btn = (Button) findViewById(R.id.Send_Btn);
        Tv = (TextView) findViewById(R.id.Tv);

        Send_Btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Send_Btn:
          //  utilsGet();
                // sendNet();
            //sendPost();
             //   ansyncSend();
                asyncPost();
                break;
        }
    }

  /*  private void utilsGet(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final String jsonStr = OkHttpUtils.getInstance().get(mUrl);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,jsonStr,Toast.LENGTH_SHORT).show();
                    }
                });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }*/

  //同步处理get方式 必须放在子线程里
 /* private void sendNet(){

      new Thread(new Runnable() {
          @Override
          public void run() {

              try {
                  OkHttpClient okHttpClient=new OkHttpClient();
                  Request request=new Request.Builder().url(mUrl).build();
                  Call call = okHttpClient.newCall(request);
                  Response response = call.execute();
                  final String string = response.body().string();
                  Log.e("string",string) ;

                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();
                      }
                  });
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }).start();

  }*/

 //同步处理post方法
 /* private void sendPost(){

      new Thread(new Runnable() {
          @Override
          public void run() {

              OkHttpClient okHttpClient=new OkHttpClient();
             //创建请求体
              RequestBody requestBody=new FormBody.Builder()
                      .add("mobile","18513426687")
                      .add("password","123456")
                      .build();
              //Request就是请求的类
              Request request =new Request.Builder().url(mUrl).build();
              Call call = okHttpClient.newCall(request);
              try {
                  Response response = call.execute();
                  final String string = response.body().string();
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();
                      }
                  });
              } catch (IOException e) {
                  e.printStackTrace();
              }

          }
      }).start();

  }*/

/*
        //异步的get方式
  private void ansyncSend(){
     OkHttpClient okHttpClient=new OkHttpClient();
     Request request=new Request.Builder().url(mUrl).build();
     Call call = okHttpClient.newCall(request);
     //异步处理
     call.enqueue(new Callback() {
         @Override
         public void onFailure(Call call, IOException e) {
             Log.e("onFailure", "onFailure");
         }

         @Override
         public void onResponse(Call call, Response response) throws IOException {

             final String string = response.body().string();

             runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                     Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();

                 }
             });

         }
     });

 }*/

private void asyncPost(){

    OkHttpClient okHttpClient=new OkHttpClient();
   RequestBody requestBody=new FormBody.Builder()
           .add("mobile","18513426687")
           .add("password","123456")
           .build();

   Request request=new Request.Builder().url(mPostUrl).post(requestBody).build();
   okHttpClient.newCall(request).enqueue(new Callback() {
       @Override
       public void onFailure(Call call, IOException e) {

       }

       @Override
       public void onResponse(Call call, Response response) throws IOException {

           final String string = response.body().string();
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();
               }
           });

       }
   });
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43668405/article/details/85156409