[技術博客]OKhttp3使用get,post,delete,patch四種請求


OKhttp3使用get,post,delete,patch四種請求

1.okhttp簡介

okhttp封裝了大量http操作,大大簡化了安卓網絡請求操作,是現在最火的安卓端輕量級網絡框架。如今okhttp已經更新到了okhttp4.0, 支持Android5.0以及以上的版本,要求Java在8.0以及以上的版本。

2.okhttp安裝

  • 可以通過添加依賴進行安裝

    implementation("com.squareup.okhttp3:okhttp:4.7.2")
    
  • 可以通過JAR的方式進行安裝,

    https://github.com/square/okhttp/
    
  • 如果使用的是androidStudio可以在Project Structure--->Dependencies 點擊“+”號選Library dependency在搜索頁面分別搜okttp,okio

3.okhttp使用

3.1get請求

這里提供官方給的例子

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  try (Response response = client.newCall(request).execute()) {
    return response.body().string();
  }
}

3.2POST請求

同樣提供官方給的例子

ublic static final MediaType JSON
    = MediaType.get("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  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();
  }
}

3.3delete請求

public class DeleteApi {
    private OkHttpClient client;
    public void Delete(final Handler handler, final String url, final int what){
        final String token = TokenPool.getTokenPool().UserToken;
        client = new OkHttpClient();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    String result = getUrl(url,token);
                    //    Log.d("TAG",result);
                    Message message1 = Message.obtain();
                    message1.what=what;
                    message1.obj = result;
                    handler.sendMessage(message1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }
    String getUrl(String url,String token) throws IOException {
        FormBody body = new FormBody.Builder().build();
        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .addHeader("Authorization","Bearer "+token)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}

3.4patch請求

public class PatchApi {
    private OkHttpClient client;
    private MediaType mediaType
            = MediaType.parse("application/json; charset=utf-8");
    public void patch(final Handler handler,final String url,final RequestBody body, final int what){
        final String token = TokenPool.getTokenPool().UserToken;
        client = new OkHttpClient();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    String result = getUrl(url,body,token);
                    Message message1 = Message.obtain();
                    message1.what= what;
                    message1.obj = result;
                    handler.sendMessage(message1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }
    String getUrl(String url, RequestBody body, String token) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization","Bearer "+token)
                .patch(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM