第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Retrofit 在 Android ( Java) 中發(fā)送 POST 請求

使用 Retrofit 在 Android ( Java) 中發(fā)送 POST 請求

紫衣仙女 2021-10-06 12:59:52
我需要從 android 應(yīng)用程序調(diào)用一個 API,但不能讓它工作.. 相同的 py 代碼工作正常.. 如何使用 Retrofit 在 Android 中移植以下 python 代碼?import requestsimport jsonhead = {'X-API-KEY':'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}args = {'item1', 'value'}c = requests.post("http_link",data=json.dumps(args),headers=head)print(c.text)如果在不使用改造庫的情況下可以使用解決方案,請分享。我嘗試通過以下方式使用改造庫在應(yīng)用程序中實現(xiàn)上述 python 代碼......接口:public interface Safety_aws_api {    String BASE_URL = "httplink";    @Headers("{X-API-KEY:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}")    @POST("Apisender")    Call<ModelApiResponse> getApi(@Body Model_ApiCaller model_apiCaller);}模型類:public class Model_ApiCaller {    public String getApiName() {        return apiName;    }    public void setApiName(String apiName) {        this.apiName = apiName;    }    private String apiName;    public Model_ApiCaller( String apiName){        this.apiName = apiName;    }    public Model_ApiCaller(){    }}public class ModelApiResponse {    private String types, Api;    public String getTypes() {        return types;    }    public void setTypes(String types) {        this.types = types;    }    public String getApi() {        return Api;    }    public void setApi(String api) {        Api = api;    }}助手類: public class RetroHelper {      private static Retrofit retrofit = new Retrofit.Builder()        .baseUrl(Safety_aws_api.BASE_URL)        .addConverterFactory(GsonConverterFactory.create())        .build();      public static Safety_aws_api api = retrofit.create(Safety_aws_api.class);}調(diào)用函數(shù):private void getCodes( Model_ApiCaller model_apiCaller) {    Call<ModelApiResponse> call = RetroHelper.api.getApi(model_apiCaller);    call.enqueue(new Callback<ModelApiResponse>() {        @Override        public void onResponse(Call<ModelApiResponse> call, Response<ModelApiResponse> response) {            tv_sample_data.setText( response.body().getApi());        }
查看完整描述

2 回答

?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

嘗試使用以下代碼添加標題。如果輸出相同,請分享您當前對 API 調(diào)用的響應(yīng)。

   Call<ModelApiResponse> getApi(@Header("X-Authorization") String apiKey,@Body Model_ApiCaller model_apiCaller);



查看完整回答
反對 回復(fù) 2021-10-06
  • 2 回答
  • 0 關(guān)注
  • 416 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號