安卓:活动时间太长,因为Web服务的HTTP请求来显示 [英] Android: Activity taking too long to display because of web service Http Request

查看:141
本文介绍了安卓:活动时间太长,因为Web服务的HTTP请求来显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个活动,做一个HTTP请求到web服务得到一些气象数据,当我启动应用程序。

该活动将采取3-4秒因为Web服务请求中显示的问题。 (测试实际设备)

我知道我米不这样做的正确的方式。所有我米做的是对的onCreate方法,我为发出请求,得到了XML回来,解析和显示数据。

什么是处理在Android的Web服务请求,这样应用程序将不会显示一个白色的屏幕发出请求,而最好的方法是什么?也许有些线程.......

我知道这是不会发生在其他的应用程序,我有我的设备,使请求来获取实时数据。

注:

1)我取回XML是没有那么大(5种元素与每一个5嵌套元素)。

2)我试图与3G网络和Wifi但响应时间仍然是相同的。

样品code:

  @覆盖
    公共无效的onCreate(包冰柱){
    super.onCreate(冰柱);
    的setContentView(R.layout.clock_weather);

   //这是它提出请求并解析XML。
    WeatherSet设定= getWeatherCondition(纽约);

    TextView的currentWeather =(TextView中)findViewById(R.id.current_weather);
    currentWeather.setText(+ set.getWeatherCurrentCondition()getTempFahrenheit());

    TextView的currentWeatherH =(TextView中)findViewById(R.id.current_weatherH);
    currentWeatherH.setText(H:+ set.getWeatherForecastConditions()得到(0).getTempMaxFahrenheit());

    TextView的currentWeatherL =(TextView中)findViewById(R.id.current_weatherL);
    currentWeatherL.setText(L:+ set.getWeatherForecastConditions()得到(0).getTempMinFahrenheit());

    ImageView的currentWeatherIcon =(ImageView的)findViewById(R.id.current_weather_icon);
    字符串IMAGEURL = set.getWeatherCurrentCondition()getIconURL()。
    可绘制bitmapDrawable = getImageBitmap(IMAGEURL);
    currentWeatherIcon.setImageDrawable(bitmapDrawable);

    setForecastInfo(套,R.id.day1,R.id.day1_icon,R.id.day1_temp,1);
    setForecastInfo(套,R.id.day2,R.id.day2_icon,R.id.day2_temp,2);
    setForecastInfo(套,R.id.day3,R.id.day3_icon,R.id.day3_temp,3);
    setForecastInfo(集,R.id.day4,R.id.day4_icon,R.id.day4_temp,4);
}
 

解决方案

的时间为你的反应是联合国predictable - 您的网络连接可以是非常差的,并采取秒传输几个字节。所以正确的方式做到这一点(如你建议)是使用线程。在我们的例子Android提供了非常有用的类来处理这样的情况 - AsynTask 。在阅读的文档,你会发现,它有3个非常强大的方法,可以帮助你

  1. 在preExecute 运行在UI线程 - 非常有帮助显示一些微调或者一些进度指示器,说明你正在做一些背景工作的用户
  2. <一个href="http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground%28Params...%29"相对=nofollow> doInBackground 在后台运行 - 在这里做你的工作背景
  3. onPostExecute 运行在在UI线程 - 当你完成你的工作背景隐藏进度,并与新接收的数据更新GUI

One of my activities make a http request to a webservice to get some weather data when I start the application.

The issue that the activity will take 3-4 seconds to display because of the webservice request. ( Tested on actual device )

I know I m not doing this the right way. All I m doing is on the onCreate method, I m making the request , getting the xml back, parsing and displaying the data.

What is the best way to deal with webservice requests in Android so the application won't display a white screen while the request is being made? Maybe some threads.......

I know this is not happening on other application I have in my device that make request to get live data.

Notes:

1) The xml I getting back is not that big ( 5 elements with 5 nested elements on each one).

2) I tried with the 3G network and Wifi but the response time is still the same.

sample code:

   @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.clock_weather);

   // this is where it is making the request and parsing the xml.
    WeatherSet set = getWeatherCondition("New York, NY");

    TextView currentWeather  = (TextView) findViewById(R.id.current_weather);
    currentWeather.setText("" + set.getWeatherCurrentCondition().getTempFahrenheit());

    TextView currentWeatherH  = (TextView) findViewById(R.id.current_weatherH);
    currentWeatherH.setText("H: " + set.getWeatherForecastConditions().get(0).getTempMaxFahrenheit());

    TextView currentWeatherL  = (TextView) findViewById(R.id.current_weatherL);
    currentWeatherL.setText("L: " + set.getWeatherForecastConditions().get(0).getTempMinFahrenheit());

    ImageView currentWeatherIcon  = (ImageView) findViewById(R.id.current_weather_icon);
    String imageUrl = set.getWeatherCurrentCondition().getIconURL();
    Drawable bitmapDrawable = getImageBitmap(imageUrl);
    currentWeatherIcon.setImageDrawable(bitmapDrawable); 

    setForecastInfo(set, R.id.day1, R.id.day1_icon, R.id.day1_temp, 1  );   
    setForecastInfo(set, R.id.day2, R.id.day2_icon, R.id.day2_temp, 2  );   
    setForecastInfo(set, R.id.day3, R.id.day3_icon, R.id.day3_temp, 3 );    
    setForecastInfo(set, R.id.day4, R.id.day4_icon, R.id.day4_temp, 4  );
}

解决方案

The time for your response is unpredictable - your network connection can be very poor and take seconds to transfer a few bytes. So the correct way to do this ( as you propose ) is to use thread. In our case android provides very useful class to handle this situations - AsynTask. After you read the docs you will notice that it has 3 very powerful methods that can help you

  1. onPreExecute runs in the ui thread - very helpful to show some spinner or some progress indicator to show the user that you are doing some work in background
  2. doInBackground runs in background - do your background work here
  3. onPostExecute runs in the ui thread- when your are done with your background work hide the progress and update the gui with the newly received data.

这篇关于安卓:活动时间太长,因为Web服务的HTTP请求来显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆