在异步中显示一个提示信息。

huangapple go评论71阅读模式
英文:

Display a toast in Async

问题

以下是翻译的内容:

所以我正在尝试这样做,如果在“EditText”中提供的链接等于特定链接,则显示一个Toast。否则,代码会继续运行,直到结果。

这是我尝试的方式(它没有检测到链接):

if (urls[0].equals("http://api.openweathermap.org/data/2.5/weather?q=null&appid=8e19904c6a1db15924eef5084a978de7")) {
    Log.e("Test", "错误!");
}

我的主要代码,如果您需要代码中的其他内容,请告诉我,我会上传:

public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(final String... urls) {

        Log.e("URL", "加载的URL = " + urls[0]);

        StringBuilder result = new StringBuilder();

        if (urls[0].equals("http://api.openweathermap.org/data/2.5/weather?q=null&amp;appid=8e19904c6a1db15924eef5084a978de7")) {
            Log.e("Test", "DSADSADASDASDAS");
        }

        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

            String line;

            while ((line = bufferedReader.readLine()) != null) {
                result.append(line).append("\n");
            }
            return result.toString();
        } catch (MalformedURLException e) {
            result.append("错误:MalformedURLException");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try {

            JSONObject jsonObject = new JSONObject(s);
            String weatherInfo = jsonObject.getString("weather");

            Log.e("JSON 数据", "" + weatherInfo);
            Toast.makeText(MainActivity.this, mCityName + "已加载", Toast.LENGTH_SHORT).show();

            JSONArray jArray = new JSONArray(weatherInfo);

            for (int i = 0; i < jArray.length(); i++) {

                JSONObject partJson = jArray.getJSONObject(i);
                mMain.setText(mCityName + "的天气是:" + partJson.getString("main"));
                mDescription.setText("还有:" + partJson.getString("description"));
                mMain.setVisibility(View.VISIBLE);
                mDescription.setVisibility(View.VISIBLE);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
英文:

So I am trying to display a toast if the link that provided in "EditText" is equal to a specific link.
Else the code keep runing until result.

Thats how I tried to do(Its not detecting the link):

    if (urls[0].equals (&quot;http://api.openweathermap.org/data/2.5/weather?q=null&amp;appid=8e19904c6a1db15924eef5084a978de7&quot;))
{
Log.e(&quot;Test&quot;,&quot;Error!&quot;);
}

My main code,if you need anything else from the code just tell me and I will upload:

public class DownloadTask extends AsyncTask<String,Void,String>{

@Override
protected String doInBackground(final String... urls) {
Log.e(&quot;URL&quot;, &quot;Loading url = &quot; + urls[0]);
StringBuilder result = new StringBuilder();
if (urls[0].equals (&quot;http://api.openweathermap.org/data/2.5/weather?q=null&amp;appid=8e19904c6a1db15924eef5084a978de7&quot;))
{
Log.e(&quot;Test&quot;,&quot;DSADSADASDASDAS&quot;);
}
URL url;
HttpURLConnection urlConnection = null;
try{
url = new URL(urls[0]);
urlConnection =  (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line).append(&quot;\n&quot;);
}
return result.toString();
} catch (MalformedURLException e) {
result.append(&quot;Error: MalformedURLException&quot;);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
@SuppressLint(&quot;SetTextI18n&quot;)
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
String weatherInfo = jsonObject.getString(&quot;weather&quot;);
Log.e(&quot;JSON data&quot;,&quot;&quot;+weatherInfo);
Toast.makeText(MainActivity.this, mCityName +&quot; has been loaded&quot;, Toast.LENGTH_SHORT).show();
JSONArray jArray = new JSONArray(weatherInfo);
for(int i = 0; 0 &lt; jArray.length(); i++){
JSONObject partJson = jArray.getJSONObject(i);
mMain.setText(&quot;The Weather in &quot; + mCityName + &quot; is: &quot; + partJson.getString(&quot;main&quot;));
mDescription.setText(&quot;And &quot; + partJson.getString(&quot;description&quot;));
mMain.setVisibility(View.VISIBLE);
mDescription.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

}

Thank you !

答案1

得分: 1

在后台线程中显示Toast需要在runOnUIThread中调用,代码如下:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(this, "URL相同", Toast.LENGTH_SHORT).show();
    }
});

这里假设你的AsincTask位于一个活动类内。

runOnUIThreadActivity类的一个方法,你需要一个Context来显示Toast

如果你的AsyncTask位于一个独立的类内,你需要提供一个Activity作为参数,或者使用Intent与一个Activity进行通信。

英文:

To show a Toast from a background thread you need to call it inside runOnUIThread like this:

runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(this, &quot;URL is the same&quot;, Toast.LENGTH_SHORT).show();
}
});

This is assuming your AsincTask is inside an activity class.

runOnUIThread is a method of Activity class, and you need a Context to show the Toast.

If your AsyncTask is in a separate class, you will need to provide an Activity as a parameter, or use an Intent to communicate with an Activity.

huangapple
  • 本文由 发表于 2020年9月15日 07:59:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63893275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定