Java 如何用截击代替异步任务

Java 如何用截击代替异步任务,java,android,json,Java,Android,Json,这是关于使用截击的Weatherforecastapp 我们如何用截击替换下面的代码 由于我们是android新手,我们发现很难实现截击 private class JSONWeatherTask extends AsyncTask<String, Void, Weather> { @Override protected Weather doInBackground(String... params) { Weather we

这是关于使用截击的Weatherforecastapp

我们如何用截击替换下面的代码

由于我们是android新手,我们发现很难实现截击

private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {

        @Override
        protected Weather doInBackground(String... params) {
            Weather weather = new Weather();
            String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1]));

            try {
                weather = JSONWeatherParser.getWeather(data);
                System.out.println("Weather ["+weather+"]");
                // Let's retrieve the icon
                weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

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

    }


    @Override
    protected void onPostExecute(Weather weather) {         
            super.onPostExecute(weather);

            if (weather.iconData != null && weather.iconData.length > 0) {
                Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length); 
                imgView.setImageBitmap(img);
            }


            cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
            temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)));
            condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");


        }
  }
私有类JSONWeatherTask扩展了AsyncTask{
@凌驾
受保护的天气背景(字符串…参数){
天气=新天气;
字符串数据=((新的WeatherHttpClient()).getWeatherData(参数[0],参数[1]);
试一试{
weather=JSONWeatherParser.getWeather(数据);
System.out.println(“天气[“+天气+”]);
//让我们检索图标
weather.iconda=((新的WeatherHttpClient()).getImage(weather.currentCondition.getIcon());
}捕获(JSONException e){
e、 printStackTrace();
}
回归天气;
}
@凌驾
执行后受保护的空(天气){
super.onPostExecute(天气);
如果(weather.iconda!=null&&weather.iconda.length>0){
位图img=BitmapFactory.decodeByteArray(weather.iconda,0,weather.iconda.length);
设置图像位图(img);
}
cityText.setText(weather.location.getCity()+”,“+weather.location.getCountry());
temp.setText(“+Math.round((weather.temperature.getTemp()-275.15));
condDescr.setText(weather.currentCondition.getCondition()+”(“+weather.currentCondition.getDescr()+”);
}
}

好消息是,截击比异步任务更容易使用!:)

Volley可以提出几种类型的请求。对于您的实现,看起来您正在检索JSON。Volley对JSONObject和JSONArray有特殊的要求,所以您可以使用对您有意义的选项

下面是如何用截击替换代码的基本概要。注意,onResponse是回调(类似于AsyncTask中的onPostExecute)

私有类WeatherTask{
public void getWeatherData(){
//创建单个队列
RequestQueue=Volley.newRequestQueue(this);
//定义您的请求
JsonObjectRequest getRequest=新的JsonObjectRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JsonObject JsonObject){
//在这里解析JSON
}
}
}
,new Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
//显示错误消息
Log.e(“错误响应:”,截击错误.toString());
}
}
);
//将其添加到请求队列
添加(getRequest);
}
}
下面是一篇关于截击的精彩演讲,了解更多信息:

private class WeatherTask{    
    public void getWeatherData() {

        // Create a single queue
        RequestQueue queue = Volley.newRequestQueue(this);

        // Define your request
        JsonObjectRequest getRequest = new JsonObjectRequest(url,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JsonObject jsonObject) {

                      // Parse JSON here

                    }
                }
            }
            , new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    // Show Error message
                    Log.e("Error Response: ", volleyError.toString());
                }
            }
         );

        // add it to the Request Queue
        queue.add(getRequest);

    }
}