亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何使用 HttpURL 連接僅發送一個請求

如何使用 HttpURL 連接僅發送一個請求

開滿天機 2022-09-07 16:07:20
我使用以下代碼向服務器發出請求。我調用了該函數一次,但請求被發送了兩次。我從服務器返回的結果來自第二個請求結果。如何只向服務器發送一個請求?我存儲所有我的:Classrunnablespublic class ServerRunnables implements Runnable {    Account currentAccount = Account.getInstance();    private String result = "";    private Context context;    private Boolean callSuccess = true;    int serviceCalled;    String email;    String password;    String username;    int coinChanges;    String urlString = "http://10.0.2.2/MobileCoin/" + services[serviceCalled];    String requiredParams;    public ServerRunnables(Context context,                           int serviceCalled,                           String email,                           String password,                           String username,                           int coinChanges) {        this.context = context;        this.serviceCalled = serviceCalled;        this.email = email;        this.password = password;        this.username = username;        this.coinChanges = coinChanges;    }    public void run() {        urlString = "http://10.0.2.2/MobileCoin/" + services[serviceCalled];        switch (serviceCalled) {            case SERVICE_RANDOM_COIN:                generateCoinServiceCalled();                break;            default:                break;        }    }    private void generateCoinServiceCalled() {        requiredParams = "email=" + email;        urlString = urlString + "?" + requiredParams;        getMethodCalled(urlString);        callSuccess = true;        String message1 = "";        if (result.equals(ServerConstants.RANDOM_COIN_EMPTY)) {            callSuccess = false;            message1 = "No Data Received";        } else if (result.equals(ServerConstants.RANDOM_COIN_FAIL)) {            callSuccess = false;            message1 = "Data Does not Exist.";        } else if (result.equals(ServerConstants.CONNECTION_ERROR)) {            callSuccess = false;            message1 = "Connection Error, Check Server";        } }
查看完整描述

2 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

為什么不使用改造來輕松處理http請求?如果與 RxJava 集成,則可以異步管理請求。


查看完整回答
反對 回復 2022-09-07
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

我發現了錯誤:

InputStream input = url.openStream();

更改為

InputStream input = hc.getInputStream();


查看完整回答
反對 回復 2022-09-07
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號