当前位置:   article > 正文

在Activity中开启一个线程执行网络操作出现的问题_activitythread: classloader.getresources: the clas

activitythread: classloader.getresources: the class loader returned by threa
由于项目需要,在Launcher中需要定位当前的城市,使用的方法是利用新浪提供的网址来获得城市。
  1. class CityThread extends Thread {
  2. @Override
  3. public void run() {
  4. getCurrentProvinceAndCity();
  5. }
  6. }
  1. private void getCurrentProvinceAndCity() {
  2. Log.d("zhangmq", "getCurrentProvinceAndCity");
  3. String provinceName = null;
  4. String cnCityName = null;
  5. final String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?";
  6. HttpGet httpGet = new HttpGet(url);
  7. Log.d("zhangmq", "getCurrentProvinceAndCity 1111111111");
  8. try {
  9. Log.d("zhangmq", "getCurrentProvinceAndCity 2222222222");
  10. HttpResponse httpResponse = new DefaultHttpClient()
  11. .execute(httpGet);
  12. Log.d("zhangmq", "getCurrentProvinceAndCity 33333333333333333333");
  13. StatusLine statusLine = httpResponse.getStatusLine();
  14. Log.d("zhangmq", "getCurrentProvinceAndCity 444444444444444444");
  15. Log.d("zhangmq","statusLine = "+statusLine);
  16. if (statusLine != null && statusLine.getStatusCode() == 200) {
  17. cityResult = EntityUtils.toString(httpResponse.getEntity())
  18. .trim();
  19. Log.d("zhangmq","cityResult = "+cityResult);
  20. cityHandler.sendEmptyMessage(GET_CITY_SUCCESS);
  21. Log.d("zhangmq","sendEmptyMessage = GET_CITY_SUCCESS");
  22. }
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. cityHandler.sendEmptyMessage(GET_CITY_FAILURE);
  26. }
  27. }
  1. private Handler cityHandler = new Handler() {
  2. @Override
  3. public void handleMessage(Message msg) {
  4. // TODO Auto-generated method stub
  5. super.handleMessage(msg);
  6. if (msg.what == GET_CITY_SUCCESS) {
  7. String[] results = cityResult.split("\t");
  8. Log.d("zhangmq", "results.length = "+results.length);
  9. if (results.length >= 5) {
  10. provinceName = results[4];
  11. cnCityName = results[5];
  12. Log.d("zhangmq", "cityHandler provinceName = " + provinceName);
  13. Log.d("zhangmq", "cityHandler cnCityName = " + cnCityName);
  14. if (mStatusBarState.getCityState()) {
  15. iv_city.setVisibility(View.VISIBLE);
  16. tv_city.setVisibility(View.VISIBLE);
  17. tv_city.setText(cnCityName);
  18. }
  19. }
  20. saveProvineAndCnCityName(provinceName, cnCityName);
  21. }
  22. }
  23. };
  1. private void saveProvineAndCnCityName(String provinceName, String cityName) {
  2. Context otherAppContext = null;
  3. try {
  4. otherAppContext = mContext.createPackageContext("com.android.msettings",
  5. Context.CONTEXT_IGNORE_SECURITY);
  6. } catch (NameNotFoundException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. SharedPreferences preferences = otherAppContext.getSharedPreferences(
  11. "share_pre", Context.MODE_MULTI_PROCESS);
  12. Editor editor = preferences.edit();
  13. editor.putString("province_name", provinceName);
  14. editor.putString("cn_city_name", cityName);
  15. editor.commit();
  16. }
在LauncherActivity中开启这个线程来获得当前城市:
new CityThread().start();
本来以为可以正确获得城市,但是结果是,根本就没有获取到。
看打印消息才知道在进行获取的时候出现了问题:
D/zhangmq ( 1979): getCurrentProvinceAndCity
D/zhangmq ( 1979): getCurrentProvinceAndCity 1111111111
D/zhangmq ( 1979): getCurrentProvinceAndCity 2222222222
W/ActivityThread( 1979): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
W/System.err( 1979): java.net.UnknownHostException: Unable to resolve host "int.dpool.sina.com.cn": No address associated with hostname
W/System.err( 1979):    at java.net.InetAddress.lookupHostByName(InetAddress.java:428)
W/System.err( 1979):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err( 1979):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err( 1979):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
W/System.err( 1979):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err( 1979):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err( 1979):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err( 1979):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 1979):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 1979):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 1979):    at com.android.mslauncher.LauncherActivityPanda.getCurrentProvinceAndCity(LauncherActivityPanda.java:647)
W/System.err( 1979):    at com.android.mslauncher.LauncherActivityPanda.run(LauncherActivityPanda.java:731)
W/System.err( 1979):    at java.lang.Thread.run(Thread.java:841)
W/System.err( 1979): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/System.err( 1979):    at libcore.io.Posix.getaddrinfo(Native Method)
W/System.err( 1979):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
W/System.err( 1979):    at java.net.InetAddress.lookupHostByName(InetAddress.java:409)
W/System.err( 1979):    ... 12 more
W/System.err( 1979): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EINVAL (Invalid argument)
W/System.err( 1979):    ... 15 more

也在网上找了好多种解决的办法,但是一直没有解决。
最后也是机缘巧合下,我把获取城市的这段代码放在一个Manager类(非Activity,普通的java类)中,然后提供接口给LauncherActivity来获得当前城市或是进行显示设置,就一切都妥妥的了。
虽然解决了,但是还是不知道根本原因。。。。。。
这里只是记录下一种解决方法。


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号