0

我正在製作一個使用互聯網數據的android應用程序。我在min sdk 2.2版上開發,但現在我將其改爲3.0。我的問題是,現在一些組件不起作用。我的猜測是,版本2.2中的某些代碼可能會在3.0中被棄用。但我不知道該怎麼做才能使它工作。ImageView適用於android min sdk 2.2,但是當我更改爲3.0時不行。

例如,我的ImageView在2.2中工作。但在3.0其不是。

  //******satellite url 
    private void satellite() { 
     // TODO Auto-generated method stub 
     ImageView imgView =(ImageView)findViewById(R.id.satellite); 
     Drawable drawable = LoadImageFromWeb("http://www.pagasa.dost.gov.ph/wb/sat_images/satellite.gif"); 
     imgView.setImageDrawable(drawable); 

    } 

另一個是我的json解析器。它也在2.2中完美工作,但不在3.0中

//json stuffs , setText on textViews in main menu 
    private void jsonStuffs() { 
     // TODO Auto-generated method stub 
    //JSON PARSER & HOME PAGE TEXTVIEWS 

     client = new DefaultHttpClient(); 
     //new Read().execute("id"); 
     //http client codes only no parser !! 
     GetMethodEx test = new GetMethodEx(); 
     String returned; 
     try { 
      returned = test.getInternetData(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try{ 
      String jsonStr = test.getInternetData(); //punta sa GetMethodEx 
       JSONObject obj = new JSONObject(jsonStr); 

       //find temperature sa JSON sa webpage 
       String temperature = obj.getString("temperature"); 
       TextView tvTemp = (TextView)findViewById(R.id.temp); 
       tvTemp.setText(temperature); 

       //find sunrise sa JSON sa webpage 
       String sunrise = obj.getString("sunrise"); 
       TextView tvSunrise = (TextView)findViewById(R.id.sunrise); 
       tvSunrise.setText("Sunrise: " + sunrise); 

       //find sunset sa JSON sa webpage 
       String sunset = obj.getString("sunset"); 
       TextView tvSunset = (TextView)findViewById(R.id.sunset); 
       tvSunset.setText("Sunset: " + sunset); 


     } 
     //catch (JSONException e) { 
      // e.printStackTrace(); 
      //} 
     catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

我還沒有收到此項目的任何錯誤。只要數據沒有顯示,該ImageView的不顯示等

我希望有人可以提供幫助。提前:)

回答

3

感謝我想原因就是Android 3.0及以上版本,你需要做的像在單獨的線程則UI線程HTTP請求和所有的網絡操作。您直接在UI線程中使用該代碼,以便在HoneyComb下運行。所以你需要在單獨的線程和UI中放入一個Web請求。 AsyncTask是一個不錯的選擇。

欲瞭解更多詳情check this link.

+0

謝謝!我認爲這可以解釋它。我會嘗試一個單獨的線程。 –

相關問題