如何在android中调用Aysnctask
我正在编写一个android应用程序,我使用Aysnctask通过openWeatherAPI获取当前天气。我怎么能叫Aysnctask类呢 这是我写的Aysnctask类:如何在android中调用Aysnctask,android,Android,我正在编写一个android应用程序,我使用Aysnctask通过openWeatherAPI获取当前天气。我怎么能叫Aysnctask类呢 这是我写的Aysnctask类: private class getWeather extends AsyncTask<String[], Void, String[]>{ @Override protected String[] doInBackground(String[]... params) { //
private class getWeather extends AsyncTask<String[], Void, String[]>{
@Override
protected String[] doInBackground(String[]... params) {
// TODO Auto-generated method stub
try {
String Url1="http://api.openweathermap.org/data/2.5/weather?lat="+currentLatitude+"&lon="+currentLongitude;
s1=getJson(Url1);
if(s1!=null){
JSONObject jObj1 = new JSONObject(s1);
Tem= jObj1.getJSONObject("main").getDouble("temp");
pressure=jObj1.getJSONObject("main").getDouble("pressure");
humm=jObj1.getJSONObject("main").getDouble("humidity");
wind=jObj1.getJSONObject("wind").getDouble("speed");
desc=jObj1.getJSONObject("weather").getDouble("description");
double tem_c=Tem-273.15;
String t=Double.toString(tem_c);
results[0]=t;
results[1]=Double.toString(pressure);
results[2]=Double.toString(humm);
results[3]=Double.toString(wind);
results[4]=Double.toString(desc);
}
} catch (Exception e) {
// TODO: handle exception
}
return results;
}//do in background
@Override
protected void onPostExecute(String[] results) {
temp.setText(results[0]+"°C");
hum.setText(results[1]+ "%");
press.setText(results[2]+ " hPa");
windSpeed.setText(results[3]+ " mps");
condDescr.setText(results[4]);
}
}
私有类getWeather扩展异步任务{
@凌驾
受保护的字符串[]doInBackground(字符串[]…参数){
//TODO自动生成的方法存根
试一试{
字符串Url1=”http://api.openweathermap.org/data/2.5/weather?lat=“+currentLatitude+”&lon=“+currentLatitude;
s1=getJson(Url1);
如果(s1!=null){
JSONObject作业J1=新的JSONObject(s1);
Tem=jObj1.getJSONObject(“main”).getDouble(“temp”);
压力=作业J1.getJSONObject(“主”).getDouble(“压力”);
humm=jObj1.getJSONObject(“主”).getDouble(“湿度”);
风=作业J1.getJSONObject(“风”).getDouble(“速度”);
desc=jObj1.getJSONObject(“天气”).getDouble(“说明”);
双tem_c=tem-273.15;
字符串t=Double.toString(temc);
结果[0]=t;
结果[1]=双倍压力;
结果[2]=双螺旋弹簧(humm);
结果[3]=双螺旋弹簧(风);
结果[4]=Double.toString(desc);
}
}捕获(例外e){
//TODO:处理异常
}
返回结果;
}//在后台做什么
@凌驾
受保护的void onPostExecute(字符串[]结果){
温度设定值(结果[0]+“°C”);
hum.setText(结果[1]+“%”);
按.setText(结果[2]+“hPa”);
风速.setText(结果[3]+“mps”);
condescr.setText(结果[4]);
}
}
当我按下按钮时,我想得到当前纬度和经度的天气,但我不知道如何执行该类
getWeather gt= new getWeather();
gt.execute(new String{currentLatitude,currentLongitude});
根据android文档中的大量示例,您可以:
new GetWeather().execute(currentLatitude,currentLongitude);
我个人会避免引用它,而是发送你需要的内容或使用听众。此外,请将您的班级大写:)
在doInBackGround
String latitutde= params[0];
String longitude = params[1];
您可以查看的文档示例如下:
new DownloadFilesTask().execute(url1, url2, url3);
正如您所看到的,不管您需要多少参数,只要从您提到的第一个通用参数开始,就可以在数组中访问它。在您的情况下,它是一个字符串
数组,因此您只能发送字符串
试试这个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
new getWeather(currentLatitude,currentLongitude).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null});
else
new getWeather(currentLatitude,currentLongitude).execute(new String[]{null});
并将构造函数添加到异步任务中
String currentLatitude;
String currentLongitude;
// constructor
@SuppressWarnings("rawtypes")
public getWeather(String currentLatitude, String currentLongitude) {
this.currentLatitude = currentLatitude;
this.currentLongitude = currentLongitude;
}
编辑1:
您不能像这样将double解析为字符串string t=double.toString(tem_c)代码>
您需要像下面这样解析代码
String t = String.valueOf(tem_c);
results[0] = t;
results[1] = String.valueOf(pressure);
results[2] = String.valueOf(humm);
results[3] = String.valueOf(wind);
results[4] = String.valueOf(desc);
我想发送当前的纬度和震级。这条路对吗?执行(新字符串{currentLatitude,currentLength})@是的,没错,我已经根据你的当前纬度和当前经度编辑了我的答案,我把它大写:)但是我怎么能发送纬度和经度?确切地说,我是如何放置的。我在查看您对另一个答案的评论后编辑了答案。AYSNTASK类型中的方法execute(字符串[]…)不适用于我阅读文档的参数(字符串,字符串),但是我一直收到我在上一篇评论中发布的错误,因为您的泛型是
将其更改为
您是否尝试过我的ans。@Tamilan是的,应用程序在此行出现空指针异常时崩溃:temp.setText(结果[0]+“°C”);应用程序崩溃:(@roa.tah where istemp
where where where where where where where where where where where where where where where where where dicular temp post that alsotemp是我声明的文本视图,但我没有编写它here@roa.tah仍然得到空指针张贴错误代码,并检查我的编辑和更改。是的,我仍然得到NPE,我认为这里有一些错误:结果[0]=t;结果[1] =String.valueOf(压力);results[2]=String.valueOf(嗡嗡声);results[3]=String.valueOf(风);results[4]=String.valueOf(desc);我试图用doBackground方法打印每条语句,但它们没有打印出来