Java:retrofit2發(fā)送http網(wǎng)絡(luò)請(qǐng)求

      網(wǎng)友投稿 1061 2025-03-31

      文檔:https://square.github.io/retrofit/

      Github: https://github.com/square/retrofit

      使用Python Flask提供簡易的api測(cè)試服務(wù)

      # -*- coding: utf-8 -*- import random from flask import Flask, Request, jsonify app = Flask(__name__) @app.route("/get") def get(): """通過get方式傳遞查詢參數(shù)""" name = Request.args.get("name") age = random.randint(10, 30) data = { "name": name, "age": age, } return jsonify(data) @app.route("/post", methods=['POST']) def post(): """通過post方式提交json數(shù)據(jù)""" name = request.json.get("name") age = random.randint(10, 30) data = { "name": name, "age": age, } return jsonify(data) if __name__ == '__main__': app.run(debug=True)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      依賴

      com.squareup.retrofit2 retrofit 2.9.0 com.squareup.retrofit2 converter-gson 2.0.0-beta3 junit junit 4.13 test

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      返回的實(shí)體對(duì)象

      package com.demo.http; public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      請(qǐng)求接口

      package com.demo.http; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query; public interface ApiService { @GET("/get") Call getPerson(@Query("name") String name); @POST("/post") Call postPerson(@Body RequestBody body); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      測(cè)試代碼

      package com.demo.http; import com.google.gson.Gson; import okhttp3.MediaType; import okhttp3.RequestBody; import org.junit.BeforeClass; import org.junit.Test; import retrofit2.Call; import retrofit2.GsonConverterFactory; import retrofit2.Response; import retrofit2.Retrofit; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class RequestTest { // 請(qǐng)求地址 private static final String BASE_URL = "http://127.0.0.1:5000/"; // json private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private static Retrofit retrofit; @BeforeClass public static void setUp() { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) // 類型轉(zhuǎn)換 Could not locate ResponseBody converter for class .addConverterFactory(GsonConverterFactory.create()) .build(); } /** * GET 測(cè)試 * * @throws IOException */ @Test public void testGet() throws IOException { ApiService request = retrofit.create(ApiService.class); Call call = request.getPerson("Tom"); // Person person = call.execute(); // 同步請(qǐng)求 Response response = call.execute(); Person person = response.body(); System.out.println(person); // Person{name='Tom', age=10} } /** * POST json 測(cè)試 * * @throws IOException */ @Test public void testPost() throws IOException { ApiService request = retrofit.create(ApiService.class); // body參數(shù) Map map = new HashMap<>(); map.put("name", "Jack"); Gson gson = new Gson(); String body = gson.toJson(map); System.out.println(body); RequestBody requestBody = RequestBody.create(body, JSON); Call call = request.postPerson(requestBody); // 同步執(zhí)行 Response response = call.execute(); Person person = response.body(); System.out.println(person); // Person{name='Tom', age=24} } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      Java:retrofit2發(fā)送http網(wǎng)絡(luò)請(qǐng)求

      82

      參考

      這是一份很詳細(xì)的 Retrofit 2.0 使用教程(含實(shí)例講解)

      HTTP Java 網(wǎng)絡(luò)

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:excel中如何快速選中第一行到最后一行(Excel如何從第一行選到最后一行)
      下一篇:如何發(fā)現(xiàn)WPS文檔中重復(fù)的段落(wps文檔查重復(fù)段落)
      相關(guān)文章
      大胆亚洲人体视频| 亚洲人成色777777老人头| 国产亚洲视频在线观看| 亚洲一本到无码av中文字幕| 亚洲一区二区三区免费在线观看| 亚洲一区二区电影| 99人中文字幕亚洲区| 亚洲综合久久综合激情久久 | 久久久无码精品亚洲日韩蜜臀浪潮| 国产精品国产亚洲精品看不卡| 亚洲日韩aⅴ在线视频| 亚洲色婷婷一区二区三区| 亚洲熟妇无码另类久久久| 亚洲人成无码网站| 亚洲国产a∨无码中文777| 久久亚洲一区二区| 亚洲综合国产精品| 亚洲国产精品乱码在线观看97| 亚洲国产精品线观看不卡| 亚洲一区二区三区免费在线观看| 国产成人精品日本亚洲专| 亚洲熟妇无码AV| 理论亚洲区美一区二区三区| 亚洲AV伊人久久青青草原 | 亚洲福利电影在线观看| 亚洲春色另类小说| 91亚洲自偷在线观看国产馆| 亚洲国产av一区二区三区丶| 亚洲久悠悠色悠在线播放| 亚洲欧美精品午睡沙发| 国产亚洲视频在线观看网址| 久久亚洲精品无码观看不卡| 亚洲妇熟XXXX妇色黄| 久久丫精品国产亚洲av| 激情五月亚洲色图| 久久精品亚洲日本波多野结衣| 亚洲精品成人网久久久久久| 亚洲三区在线观看无套内射| 亚洲国产女人aaa毛片在线 | 亚洲精品免费在线视频| 亚洲成a人片在线看|