英文:
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&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 ("http://api.openweathermap.org/data/2.5/weather?q=null&appid=8e19904c6a1db15924eef5084a978de7"))
{
Log.e("Test","Error!");
}
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("URL", "Loading url = " + urls[0]);
StringBuilder result = new StringBuilder();
if (urls[0].equals ("http://api.openweathermap.org/data/2.5/weather?q=null&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("Error: 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 data",""+weatherInfo);
Toast.makeText(MainActivity.this, mCityName +" has been loaded", Toast.LENGTH_SHORT).show();
JSONArray jArray = new JSONArray(weatherInfo);
for(int i = 0; 0 < jArray.length(); i++){
JSONObject partJson = jArray.getJSONObject(i);
mMain.setText("The Weather in " + mCityName + " is: " + partJson.getString("main"));
mDescription.setText("And " + partJson.getString("description"));
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
位于一个活动类内。
runOnUIThread
是Activity
类的一个方法,你需要一个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, "URL is the same", 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论