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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將從遠(yuǎn)程 url 返回的字符串分配給變量

將從遠(yuǎn)程 url 返回的字符串分配給變量

呼喚遠(yuǎn)方 2022-07-14 09:23:31
我正在嘗試將遠(yuǎn)程 URL 中的字符串分配給變量。當(dāng)我在運(yùn)行遠(yuǎn)程 URL 代碼后檢查該變量時(shí),它是空的。首先我聲明了空字符串,然后從 URL 中獲取字符串并嘗試將其分配給變量。字符串被提取但未分配給變量。下面是代碼public class MainActivity extends Activity {static String channel_uri = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    new DownloadWebPageTask().execute();    if(channel_uri.isEmpty()){        Log.i("channel_text", "Empty");    }}private static class DownloadWebPageTask extends AsyncTask<String, Void, String> {    @Override    protected String doInBackground(String... params) {        final OkHttpClient client = new OkHttpClient();        Request request = new Request.Builder()                .url("http://yeshuaatv.com/channel/streamingurl/adaptive.txt")                .build();        Response response = null;        try {            response = client.newCall(request).execute();            if (!response.isSuccessful())                throw new IOException("Unexpected code " + response);            Headers responseHeaders = response.headers();            channel_uri = response.body().string();            return channel_uri;        } catch (IOException e) {            e.printStackTrace();        }        return null;    }    @Override    protected void onPostExecute(String s) {        //tvdata.setText(channel);        // you will get data in url string        super.onPostExecute(s);    }}}
查看完整描述

2 回答

?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

問(wèn)題是該execute()方法在新線程中啟動(dòng)任務(wù)并異步執(zhí)行此操作。這意味著當(dāng) HTTP 請(qǐng)求發(fā)生時(shí),您的代碼onCreate會(huì)繼續(xù)運(yùn)行,并且您的if檢查會(huì)在請(qǐng)求完成之前進(jìn)行。


要解決此問(wèn)題,您必須等待請(qǐng)求完成并在那里執(zhí)行您的代碼。為此,AsyncTask您可以覆蓋onPostExecute在任務(wù)完成后在 UI 線程上運(yùn)行的內(nèi)容。


您的代碼將如下所示:


@Override

protected void onPostExecute(String channel_uri) {

    if(channel_uri.isEmpty()){

        Log.i("channel_text", "Empty");

    }

}

這也應(yīng)該刪除對(duì) channel_uri 的類成員的使用。因?yàn)槟潜粋鬟f到onPostExecute


查看完整回答
反對(duì) 回復(fù) 2022-07-14
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

“onCreate”和“doInBackground”在不同的線程中運(yùn)行。所以這段代碼


 if(channel_uri.isEmpty()){

    Log.i("channel_text", "Empty");

}

在您收到 AsyncTask 中的響應(yīng)之前執(zhí)行。


這就是它被稱為 AsyncTask 的原因。當(dāng)您收到響應(yīng)時(shí),您需要在 doInBackground 中記錄 channel_uri。


查看完整回答
反對(duì) 回復(fù) 2022-07-14
  • 2 回答
  • 0 關(guān)注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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