手把手用Android stdio寫OKHTTP java工程demo

創(chuàng)建工程

1,在AS中創(chuàng)建普通android工程
2,創(chuàng)建一個(gè)module

創(chuàng)建module

選擇Java or Kotlin Library

3,引入okhttp庫
我這邊module取名為“okhttpTest”,找到modulel下的build.gradle,加入依賴:

implementation 'com.squareup.okhttp3:okhttp:4.4.0'
插入依賴

請到https://github.com/square/okhttp查看最新的版本號:

github查詢最新版本

4,module運(yùn)行配置
我們先加入main函數(shù):

package com.example.okhttptest;

import java.io.IOException;

public class OKHttpTest {

    public static void main(String[] args) throws IOException {
        System.out.println("hello");
    }
}

正常情況下,直接執(zhí)行“Run”就可以執(zhí)行了。如果有問題,可以手動(dòng)配置:


打開運(yùn)行配置界面
運(yùn)行配置

配置后,就可以執(zhí)行“Run”了,檢查打印是否正常:


檢查運(yùn)行后打印是否正常

OK,目前工程已經(jīng)正常創(chuàng)建。

編寫OKHTTP測試代碼

Get / Post測試

package com.example.okhttptest;

import java.io.IOException;

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

public class OKHttpTest {
    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    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();
        }
    }

    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();
        }
    }

    String bowlingJson(String player1, String player2) {
        return "{'winCondition':'HIGH_SCORE',"
                + "'name':'Bowling',"
                + "'round':4,"
                + "'lastSaved':1367702411696,"
                + "'dateStarted':1367702378785,"
                + "'players':["
                + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
                + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
                + "]}";
    }

    public static void main(String[] args) throws IOException {
        OKHttpTest example = new OKHttpTest();
        String responseGet = example.run("https://github.com/square/okhttp/blob/master/README.md");
        System.out.println(responseGet);

        String json = example.bowlingJson("Jesse", "Jake");
        String responsePost = example.post("http://www.roundsapp.com/post", json);
        System.out.println(responsePost);
    }
}

運(yùn)行一下,我們就可以通過Get / Post獲得返回的HTTP源碼了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容