出了什么问题:app在某些设备上冻结了吗?

时间:2016-12-13 05:26:48

标签: android android-studio-2.2

我一直在开发一款应用程序,并且在某些设备中存在性能问题,例如配置相同但品牌差异。 我正在使用两个设备lenovo和三星。与相同的处理器相同的RAM相同的ROM。 这个应用程序在它挂起的三星设备上表现不同,在联想它的工作正常。 我的代码有什么问题,或者其他人遇到同样的问题并找到了解决方法吗?

//截击请求

 private void request() {
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url + "abc.php",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            systemm.out("Daily Dose"+response);
                            try {
                                JSONArray jArray = new JSONArray(response);
                                int count = jArray.length();
                                for (int i = 0; i <= count; i++) {
                                    JSONObject j = jArray.getJSONObject(i);

                                    Item itemm=new Item();
                                    itemm.setUrlPic(j.getString("image"));
                                    itemm.setPicCount(j.getString("imcount"));
                                    itemm.setName(CapitalLetter.AllFirstLetterWithBlanckSpace(Basee64.Decode2(j.getString("fname"))+" "
                                            +Basee64.Decode2(j.getString("lname"))));
                                    itemm.setAbout(CapitalLetter.AllFirstLetterWithBlanckSpace(Basee64.Decode2(j.getString("fullabout"))));
                                    itemm.setAgeHeight(j.getString("age")+","
                                            +Basee64.Decode2(j.getString("height")));
                                    itemm.setMaritalStatus(Basee64.Decode2(j.getString("marital")));
                                    itemm.setReligion(Basee64.Decode2(j.getString("religion")));
                                    itemm.setEat(Basee64.Decode2(j.getString("food")));
                                    itemm.setDrink(Basee64.Decode2(j.getString("drink")));
                                    itemm.setSmoke(Basee64.Decode2(j.getString("smoke")));
                                    itemm.setLocation(CapitalLetter.FirstLetter(Basee64.Decode2(j.getString("city")))+","
                                            +CapitalLetter.FirstLetter(Basee64.Decode2(j.getString("state")))+","
                                            +CapitalLetter.FirstLetter(Basee64.Decode2(j.getString("country"))));
                                    itemm.setQualification(Basee64.Decode2(j.getString("education")));
                                    itemm.setAnnualIncome(Basee64.Decode2(j.getString("income")));
                                    items.add(itemm);
                                    show(page);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            systemm.out("Daily Dose"+error.getMessage());

                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("uid", "1");
                    return params;
                }

            };
            RequestQueue requestQueue = Volley.newRequestQueue(activityy);
            requestQueue.add(stringRequest);
        }

        private void show(int i) {

            Item it=items.get(i);
            nameTv.setText(it.getName());
            aboutTv.setText(it.getAbout());
            age_height_Tv.setText(it.getAgeHeight());
            maritalStatusTv.setText(it.getMaritalStatus());
            religionTv.setText(it.getReligion());
            eatTv.setText(it.getEat());
            drinkTv.setText(it.getDrink());
            smokeTv.setText(it.getSmoke());
            locationTv.setText(it.getLocation());
            QualificationTv.setText(it.getQualification());
            AnnualIncomeTv.setText(it.getAnnualIncome());
            picCountTv.setText(it.getPicCount());

            //String urll="http://ste.india.com/sites/default/files/2015/12/18/442851-max.jpg";
            //String urll="http://www.fashioncentral.pk/images/models/111/Sanam_Jung_Pic.jpg";
            imageLoader = CustomVolleyRequest.getInstance(activityy.getApplicationContext()).getImageLoader();
            imageLoader.get(Config.urlImage+it.getUrlPic(), ImageLoader.getImageListener(profilePicIv, R.drawable.blur, android.R.drawable.ic_dialog_alert));
            profilePicIv.setImageUrl(Config.urlImage+it.getUrlPic(), imageLoader);



        }

//自定义排球

public class CustomVolleyRequest {

    private static CustomVolleyRequest customVolleyRequest;
    private static Context context;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;


    private CustomVolleyRequest(Context context) {
        this.context = context;
        this.requestQueue = getRequestQueue();

        imageLoader = new ImageLoader(requestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized CustomVolleyRequest getInstance(Context context) {
        if (customVolleyRequest == null) {
            customVolleyRequest = new CustomVolleyRequest(context);
        }
        return customVolleyRequest;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            requestQueue = new RequestQueue(cache, network);
            requestQueue.start();
        }
        return requestQueue;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }

}

//大写字母

public class CapitalLetter {
    public static String AllFirstLetterWithBlanckSpace(String str) {
        String[] strArray = str.split(" ");
        StringBuilder builder = new StringBuilder();
        for (String s : strArray) {
            String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
            builder.append(cap + " ");
        }


        return builder.toString();
    }


        public static String FirstLetter(String str) {
            String[] strArray = str.split(" ");
            StringBuilder builder = new StringBuilder();
            for (String s : strArray) {
                String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
                builder.append(cap + "");
            }


            return builder.toString();
        }

        public static String Peragrap(String str) {
            String[] strArray = str.split(".");
            StringBuilder builder = new StringBuilder();
            for (String s : strArray) {
                String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
                builder.append(cap + ".");
            }


            return builder.toString();
        }
    }

// base64编码解码

public class Basee64 {


    public static String Decode(String s) {

        String data=null;
        if(s!=null) {
            try {
                data = new String(Base64.decode(s, Base64.DEFAULT), "UTF-8").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    public static String Encode(String s) {

        String data=null;

        if(s!=null) {
            try {
                data = Base64.encodeToString(s.getBytes("UTF-8"), Base64.DEFAULT).trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return data;
    }




     public static String Decode2(String s) {
            String data=null;
            if(s!=null) {
                try {
                    data = new String(Base64.decode(new String(Base64.decode(s, Base64.DEFAULT), "UTF-8"), Base64.DEFAULT), "UTF-8").trim();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return data;
        }

        public static String Encode2(String s) {
            String data=null;

            if(s!=null) {
                try {
                    data = Base64.encodeToString(Base64.encodeToString(s.getBytes("UTF-8"), Base64.DEFAULT).trim().getBytes("UTF-8"), Base64.DEFAULT).trim();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
    }

4 个答案:

答案 0 :(得分:0)

尝试使用

        Runtime.getRuntime().maxMemory();

你的代码被卡住了 因为有些手机没有自动清除RAM,但你的应用程序需要更多内存,这样你可以为你的操作分配更多的内存。清除

下面的内存使用情况
  System.runFinalization();
    Runtime.getRuntime().gc();
    System.gc();

//清除内存

答案 1 :(得分:0)

以上答案很棒。但是编程方面,你必须做更多像线程,服务处理程序的事情。如果您在主线程中编码所有内容,它将一直挂起,直到完成该过程。但是把非ui的东西放到后台线程或异步任务中。

答案 2 :(得分:0)

如果应用程序冻结的时间足以获得应用程序无响应(ANR)弹出窗口,那么您可以拉出traces.txt文件以查看确切的内容:

adb pull /data/anr/traces.txt

该文件将包含应用程序的线程转储。看看主线程正在做什么。这将是一个暗示应该放在后台线程上的内容。

答案 3 :(得分:0)

回复有多大?膨胀json并不便宜,如果你的json非常大,这意味着应用程序可能需要一些时间来创建适当的对象来代表你的json。如果这是尝试使用JsonReader充气json的原因。