Java 从url加载图像速度变慢系统android

Java 从url加载图像速度变慢系统android,java,json,eclipse,android-fragments,android-imageview,Java,Json,Eclipse,Android Fragments,Android Imageview,每当我想在手机上运行我的代码时,手机速度很慢,图像加载也很慢,我认为这是尺寸问题,所以请告诉我如何获得小尺寸的图像。 这是我的密码 public class TopRatedFragment extends Fragment { JSONParser jParser = new JSONParser(); private static String url_all_products = "http://3ilogics.org/vid/get_all_products.php"

每当我想在手机上运行我的代码时,手机速度很慢,图像加载也很慢,我认为这是尺寸问题,所以请告诉我如何获得小尺寸的图像。 这是我的密码

public class TopRatedFragment extends Fragment {

    JSONParser jParser = new JSONParser();
    private static String url_all_products = "http://3ilogics.org/vid/get_all_products.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "price";
    private static final String TAG_IMG = "images_url";

    // products JSONArray
    JSONArray products = null;
    JSONArray products2 = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container,
                false);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        try {
            products = json.getJSONArray(TAG_PRODUCTS);



            //sd=json.getJSONObject(TAG_PRODUCTS).length();
            for (int i = 0; i < products.length(); i++) {
                JSONObject c = products.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_PID);
                String name = c.getString(TAG_NAME);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_PID, id);
                map.put(TAG_NAME, name);


            }


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HorizontalListView listview = (HorizontalListView) rootView
                .findViewById(R.id.listview);
        listview.setAdapter(mAdapter);


        return rootView;


    }

    private static String[] dataObjects = new String[] { "Text #1", "Text #2",
            "Text #3", "Text #4", "Text #5", "Text #6", "Text #7", "Text #8",
            "Text #9", "Text #10" };

    private BaseAdapter mAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return TopRatedFragment.this.products.length();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View retval = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.viewitem, null);
            TextView title = (TextView) retval.findViewById(R.id.title);
            ImageView title2 = (ImageView) retval.findViewById(R.id.image);
            String img=null;

            try {
                //Log.d("sda",products.getJSONObject(position).toString());
                title.setText(products.getJSONObject(position).getString(TAG_NAME));
                img=products.getJSONObject(position).getString(TAG_IMG);
                title2.setImageBitmap(getBitmapFromURL(img));
                System.gc();
            } 

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



            //Log.d("All Products: ", json.toString());
            return retval;
        }

    };
    public Bitmap getBitmapFromURL(String img) {
        try {

            URL url = new URL(img);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();

            Bitmap myBitmap = BitmapFactory.decodeStream(input);

            return myBitmap;

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
    }


}
公共类TopRatedFragment扩展了片段{
JSONParser jParser=新的JSONParser();
私有静态字符串url\u所有产品=”http://3ilogics.org/vid/get_all_products.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“PRODUCTS”;
私有静态最终字符串标记_PID=“PID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串TAG_EMAIL=“price”;
私有静态最终字符串标记\u IMG=“images\u url”;
//产品JSONArray
JSONArray产品=null;
JSONArray products2=null;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u top\u rated,容器,
假);
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
试一试{
products=json.getJSONArray(TAG_products);
//sd=json.getJSONObject(TAG_PRODUCTS).length();
对于(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
}
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
HorizontalListView列表视图=(HorizontalListView)根视图
.findviewbyd(R.id.listview);
setAdapter(mAdapter);
返回rootView;
}
私有静态字符串[]数据对象=新字符串[]{“文本#1”、“文本#2”,
“文本3”、“文本4”、“文本5”、“文本6”、“文本7”、“文本8”,
“文本#9”、“文本#10”};
专用BaseAdapter mAdapter=新BaseAdapter(){
@凌驾
public int getCount(){
返回TopRatedFragment.this.products.length();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
View retval=LayoutInflater.from(parent.getContext()).flate(
R.layout.viewitem,空);
TextView title=(TextView)retval.findViewById(R.id.title);
ImageView标题2=(ImageView)retval.findViewById(R.id.image);
字符串img=null;
试一试{
//Log.d(“sda”,products.getJSONObject(position.toString());
title.setText(products.getJSONObject(position.getString(TAG_NAME));
img=products.getJSONObject(position).getString(TAG\u img);
标题2.setImageBitmap(getBitmapFromURL(img));
gc();
} 
捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//Log.d(“所有产品:,json.toString());
返回返回;
}
};
公共位图getBitmapFromURL(字符串img){
试一试{
URL=新URL(img);
HttpURLConnection连接=(HttpURLConnection)url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream输入=连接。getInputStream();
位图myBitmap=BitmapFactory.decodeStream(输入);
返回我的位图;
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
返回null;
}
}
}

使用异步任务执行此类操作,类似这样的内容:

由于网络调用无法在UI线程中执行,所以在运行此代码时应该会出现异常。如何使用异步任务您可以通过任何示例进行解释吗code@user3345828如果您只需搜索AsyncTask,您应该能够找到数百万个示例/教程。。。