标签:
9.取消使用Volley库改用基本HttpURLConnection
在使用Volley库的地方使用AsyncTask来替换,使用HttpURLConnection来进行数据加载
需要更改的地方是UpdateService和MainActivity:
1 private void update(){ 2 Log.d(TAG,"在Update服务中启动"); 3 if(!configure.exists()){ 4 Toast.makeText(getApplicationContext(), R.string.fetching, Toast.LENGTH_SHORT).show(); 5 new FetchAsyncTask().execute(url); 6 }else{ 7 try{ 8 hasNew = !(DateTools.getDate().equals(FileTools.loadConf(getApplicationContext()))); 9 }catch (IOException e){ 10 e.printStackTrace(); 11 } 12 if(hasNew){ 13 Toast.makeText(getApplicationContext(),R.string.fetching,Toast.LENGTH_SHORT).show(); 14 new FetchAsyncTask().execute(url); 15 } 16 } 17 } 18 19 private class FetchAsyncTask extends AsyncTask<String,Void,String> { 20 21 @Override 22 protected String doInBackground(String... params) { 23 BufferedReader reader; 24 StringBuilder builder = new StringBuilder(); 25 try{ 26 URL url=new URL(params[0]); 27 HttpURLConnection connection=(HttpURLConnection)url.openConnection(); 28 InputStream in=connection.getInputStream(); 29 reader=new BufferedReader(new InputStreamReader(in)); 30 String line; 31 while ((line = reader.readLine())!=null){ 32 builder.append(line); 33 } 34 } catch(IOException e){ 35 e.printStackTrace(); 36 } 37 return builder.toString(); 38 } 39 40 @Override 41 protected void onPostExecute(String s) { 42 Log.d(TAG, s); 43 try{ 44 ArrayList<WeatherItem> weatherItems = ParseTools.getInstance(s, ParseTools.REQUEST_RAW); 45 46 //保存数据到文件 47 FileTools.saveData(getApplicationContext(), weatherItems); 48 Toast.makeText(getApplicationContext(),"更新完毕",Toast.LENGTH_SHORT).show(); 49 50 FileTools.saveWidgetData(getApplicationContext(), weatherItems.get(0)); 51 52 Intent intent=new Intent(getApplicationContext(),UpdateService.class); 53 startService(intent); 54 }catch (IOException | JSONException e){ 55 e.printStackTrace(); 56 } 57 super.onPostExecute(s); 58 } 59 }
和:
1 private void update(){ 2 Log.d(TAG,"在主UI中启动"); 3 //只有第一次加载数据还没有下载完成的时候才会有mAdapter为空的情况 4 if(mAdapter == null){ 5 mAdapter = new ItemAdapter(mWeatherItems); 6 listView.setAdapter(mAdapter); 7 } 8 9 if(!configure.exists()){ 10 Toast.makeText(getApplicationContext(),R.string.fetching,Toast.LENGTH_SHORT).show(); 11 new FetchAsyncTask().execute(url); 12 }else{ 13 try{ 14 hasNew = !(DateTools.getDate().equals(FileTools.loadConf(getApplicationContext()))); 15 }catch (IOException e){ 16 e.printStackTrace(); 17 } 18 if(hasNew){ 19 Toast.makeText(getApplicationContext(),R.string.fetching,Toast.LENGTH_SHORT).show(); 20 new FetchAsyncTask().execute(url); 21 }else{ 22 try{ 23 mWeatherItems=FileTools.loadData(getApplicationContext()); 24 mAdapter = new ItemAdapter(mWeatherItems); 25 listView.setAdapter(mAdapter); 26 }catch (IOException | JSONException e){ 27 e.printStackTrace(); 28 } 29 } 30 } 31 } 32 33 private class FetchAsyncTask extends AsyncTask<String,Void,String>{ 34 35 @Override 36 protected String doInBackground(String... params) { 37 BufferedReader reader; 38 StringBuilder builder = new StringBuilder(); 39 try{ 40 URL url=new URL(params[0]); 41 HttpURLConnection connection=(HttpURLConnection)url.openConnection(); 42 InputStream in=connection.getInputStream(); 43 reader=new BufferedReader(new InputStreamReader(in)); 44 String line; 45 while ((line = reader.readLine())!=null){ 46 builder.append(line); 47 } 48 } catch(IOException e){ 49 e.printStackTrace(); 50 } 51 return builder.toString(); 52 } 53 54 @Override 55 protected void onPostExecute(String s) { 56 Log.d(TAG,s); 57 try{ 58 mWeatherItems = ParseTools.getInstance(s,ParseTools.REQUEST_RAW); 59 60 //保存数据到文件 61 FileTools.saveData(getApplicationContext(), mWeatherItems); 62 Toast.makeText(getApplicationContext(),"更新完毕",Toast.LENGTH_SHORT).show(); 63 64 mAdapter=new ItemAdapter(mWeatherItems); 65 listView.setAdapter(mAdapter); 66 67 FileTools.saveWidgetData(getApplicationContext(), mWeatherItems.get(0)); 68 69 Intent intent=new Intent(getApplicationContext(),UpdateService.class); 70 startService(intent); 71 }catch (IOException | JSONException e){ 72 e.printStackTrace(); 73 } 74 super.onPostExecute(s); 75 } 76 }
因为如果Volley这个库的方法,我们获取的同样是这些字符串,因此与其从JSONObject转换成字符串还不如直接把字符串加载出来。
在之前就发现,当在运行过程中更改date.conf(模拟日期更改)的时候,会出现这样一个异常:
java.lang.RuntimeException: Handler (android.os.Handler) {415a25e0} sending message to a Handler on a dead thread
下一阶段就是考虑怎么解决这个问题。
标签:
原文地址:http://www.cnblogs.com/lhyz/p/4319810.html