我愛java系列之---【發送請求消息的中間件—okhttp】

使用步驟:java

(1)導入依賴ide

    <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>

(2)java代碼ui

String url = "http://192.168.200.128/ad_update?position="+position;
        //建立okhttp對象,這個東西是目前最快的發送請求的中間件技術
        OkHttpClient okHttpClient = new OkHttpClient();
        //建立請求對象
        Request request = new Request.Builder().url(url).build();
        //發送請求
        Call call = okHttpClient.newCall(request);

        //調用發送請求後的回調方法,獲取發送成功或者是失敗的消息
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                System.out.println("=======發送失敗========");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("=======發送成功======"+response.message());
            }
        });
相關文章
相關標籤/搜索