android中引入okhttp

选择在低版本中运行的,android 7
代码通过学习  https://square.github.io/okhttp/ 中的

sAndroidManifest.xml 中新增权限

android中引入okhttp_第1张图片

 

引入 okIo 和okhttp 包

android中引入okhttp_第2张图片

android中引入okhttp_第3张图片

 

搜所okio

 

android中引入okhttp_第4张图片

 

 

android中引入okhttp_第5张图片

 


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.a202007.R;
import com.example.a202007.com.mw.dto.SearchDTO;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkhttpActivity extends AppCompatActivity {
    private TextView okHttp_showcontent;
    public static final MediaType JSON= MediaType.get("application/json; charset=utf-8");
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.i("mw",String.valueOf(msg.obj));
            okHttp_showcontent.setText((String)msg.obj);
        }
    };
    OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        okHttp_showcontent = findViewById(R.id.okHttp_showcontent);
    }

    public void onOkhttpGetOnClick (View view) throws IOException {
        okHttp_showcontent.setText("");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.obj = getReq("http://www.mwview.com/v/tool/queryFoodMenuList");
                handler.sendMessage(msg);
            }
        }).start();
    }

    public void onOkhttpPostOnClick (View view) throws IOException {
        okHttp_showcontent.setText("");
        new Thread(new Runnable() {
            @Override
            public void run() {
                SearchDTO search = new SearchDTO();
                search.setPageNumber(1);
                search.setPageSize(20);
                search.setSearchProperty("categoryId");
                search.setSearchValue("SUBJECT_DETAIL_CATEGORIES_LITERATURE_ART");
                String jsonString = new Gson().toJson(new Object());
                Message msg = new Message();
                msg.obj = postReq("http://www.mwview.com/v/blog/queryBlogList",jsonString);
                handler.sendMessage(msg);
            }
        }).start();
    }


   public String postReq(String url, String json){
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }catch (IOException e){
            Log.e("mw","post=="+e.getMessage());
        }
        return "";
    }


    private String getReq(String url)  {
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }catch (IOException e){
            Log.e("mw",e.getMessage());
        }
        return "";
    }

}





    

    

 

android中引入okhttp_第6张图片

你可能感兴趣的:(android,okhttp,OKHttp3)