문제

I'm making an android application that uses internet data. I developed on min sdk version 2.2 but now I changed it to 3.0. My problem is that now some of the components don't work. My guess is that some code in version 2.2 might be deprecated in 3.0. But I have no idea what to do to make it work.

For example, my ImageView is working in 2.2. But in 3.0 its not.

            //******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);

    }

Another is my json parser. It also work perfectly in 2.2 but not in 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();
            }
    }

I haven't received any errors on this project. Just the data is not displaying, the imageview is not displaying etc.

I hope someone can help. Thanks in advance :)

도움이 되었습니까?

해결책

I think the reason is Android 3.0 and above, you need to do network operation like http request and all in seperate thread then the UI thread. You are directly using the code in UI thread so it works below HoneyComb. So you need to put a web request in seperate thread then UI. AsyncTask is a good option.

For more detail check this link.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top