静态方法中的垃圾收集

时间:2011-07-08 19:16:53

标签: android garbage-collection

我注意到我的应用程序使用的内存不断增加,我怀疑它是由于大量使用我的httpclient所示。那会有意义吗?我应该删除方法中创建的所有对象(HttpPost,HttpResponse,HttpEntity,String ...)?

由于

public class RestJsonClient {

    private static final String TAG = "RestJsonClient";

    // Thread safe httpclient
    // http://metatroid.com/articles/Android%20Development(part%201)%20-%20Syncing%20Cookies%20Between%20Http%20Clients%20and%20WebViews
    private static final DefaultHttpClient client = createClient();   

    private static DefaultHttpClient createClient(){
        BasicHttpParams params = new BasicHttpParams();
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
        httpclient.getCookieStore().getCookies();
        return httpclient;
    }

    static HttpContext localContext = new BasicHttpContext();

    public static JSONObject getJSONObject(String url)
    {
        return getJSONObject(url, null);
    }

    public static JSONObject getJSONObject(String url, List<NameValuePair> postData)
    {
        return getJSONObject(url, postData, true);
    }

    public static JSONObject getJSONObject(String url, List<NameValuePair> postData, boolean useGlobalContext)
    { 

        // Prepare a request object
        HttpPost httppost = new HttpPost(url); 

        // Execute the request
        HttpResponse response;

        JSONObject json = new JSONObject();

        try {

            if(postData!=null)
                httppost.setEntity(new UrlEncodedFormEntity(postData));

            if(useGlobalContext){
                response = client.execute(httppost, localContext);
            }
            else{
                response = client.execute(httppost);
            }
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 200) {

                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    // JSON Response Read                    
                    String result = EntityUtils.toString(entity, HTTP.UTF_8);
                    json = new JSONObject(result);
                }

            } else {
                Log.e(TAG, "status!=200 for url " + url + "and post data " + postData);
            }

        } catch (ClientProtocolException e) {           
            e.printStackTrace();
        } catch (IOException e) {            
            e.printStackTrace();
        } catch (JSONException e) {            
            e.printStackTrace();
        }

        return json;
    } 
}

0 个答案:

没有答案
相关问题