1

每次我运行这段代码时,我都会强制关闭,我不明白。我是Android开发新手,有人可以帮忙吗?我真的很感谢你们的任何提示。到目前为止,我只是明白,内存空间快用完了,但是我怎样才能防止内存不足呢?

我的代码:

    Boolean start = true;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePair;
    TextView txtEntries;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        txtEntries = (TextView)findViewById(R.id.txtEntries);
        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String User = p.getString("User", "");
        String Password = p.getString("Password", "");
        txtEntries.setText("user:" + User + "Passwort:" + Password);
        getEntries.start();
        start = true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main_screen, menu);
        return true;
    }

    Thread getEntries = new Thread() {
        public void run() {
            //while(!login_thread.isInterrupted()) {
            while (getEntries.isInterrupted() == false) {
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (start) {
                    //Code for login+Action from saved text
                    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    String User = p.getString("User", "");
                    String Password = p.getString("Password", "");
                    login("Entries", User, Password);
                }
            }
            //while (start == true){
            //login(box_User.getText().toString(), box_Password.getText().toString());
            //}
            //}
        }
    };

    //implement Handler

    Handler handler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              getEntries.interrupt();
             txtEntries = (TextView)findViewById(R.id.txtEntries);
             txtEntries.setText("Handler");
              Bundle bundle = msg.getData();
              Boolean Connection = bundle.getBoolean("Connection");
              String response = bundle.getString("Entries");
              if (Connection) {
                 // Toast.makeText(getBaseContext(), "Connection", 5000).show();
                  if (response != "") {
                      //Code which shows all entries
                      Toast.makeText(getBaseContext(), "Response", 5000).show();
                      txtEntries.setText(response);

                  } else {
                      txtEntries.setText("Empty");
                  }
              }else {
                  txtEntries.setText("No connection");
                  getEntries.interrupt();
              }
          }
    };

    // getData method
    private void login(String Action, String User, String Password) {
        // TODO Auto-generated method stub
            start = false;
            Message msg = handler.obtainMessage();
            Bundle bundle = new Bundle();
            httppost = new HttpPost("http://10.0.2.2/KHG/api/check-login.php");
            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            // The default value is zero, that means the timeout is not used. 
            int timeoutConnection = 15000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 15000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            httpclient = new DefaultHttpClient(httpParameters);
            nameValuePair = new ArrayList<NameValuePair>(1);
            nameValuePair.add(new BasicNameValuePair("User", User));
            nameValuePair.add(new BasicNameValuePair("Password", Password));
            nameValuePair.add(new BasicNameValuePair("Action", Action));
            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                e.printStackTrace();
                bundle.putBoolean("Connection", false);
                bundle.putString("Entries", "");
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
            try {
                response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                bundle.putBoolean("Connection", false);
                bundle.putString("Entries", "");
                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                bundle.putBoolean("Connection", false);
                bundle.putString("Entries", "");
                msg.setData(bundle);
                handler.sendMessage(msg);
            } 
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response;
            try {
            //  Toast.makeText(getBaseContext(), "test", Toast.LENGTH_LONG).show();
                response = httpclient.execute(httppost, responseHandler);
                SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                p.edit().putString("Data", response).commit();
                bundle.putBoolean("Connection", true);
                bundle.putString("Entries", "ok");
                msg.setData(bundle);
                handler.sendMessage(msg);

            }
            catch (Exception e) {
                e.printStackTrace();
                bundle.putBoolean("Connection", false);
                bundle.putString("Entries", "");
                msg.setData(bundle);
                handler.sendMessage(msg);
            //  Toast.makeText(getBaseContext(), "exception", Toast.LENGTH_LONG).show();
            }

        }
}

还有我的日志:

03-23 16:32:22.683: D/dalvikvm(741): GC_FOR_ALLOC freed 135K, 4% free 8821K/9159K, paused 60ms
03-23 16:32:22.822: W/SingleClientConnManager(741): Invalid use of SingleClientConnManager: connection still allocated.
03-23 16:32:22.822: W/SingleClientConnManager(741): Make sure to release the connection before allocating another one.
03-23 16:32:23.012: W/System.err(741): java.lang.InterruptedException
03-23 16:32:23.012: W/System.err(741):  at java.lang.VMThread.sleep(Native Method)
03-23 16:32:23.012: W/System.err(741):  at java.lang.Thread.sleep(Thread.java:1214)
03-23 16:32:23.022: W/System.err(741):  at java.lang.Thread.sleep(Thread.java:1196)
03-23 16:32:23.022: W/System.err(741):  at com.shr.khg.MainActivity$2.run(MainActivity.java:109)
03-23 16:32:25.342: D/dalvikvm(741): GC_FOR_ALLOC freed 80K, 3% free 9148K/9415K, paused 60ms
03-23 16:32:25.442: W/SingleClientConnManager(741): Invalid use of SingleClientConnManager: connection still allocated.
03-23 16:32:25.442: W/SingleClientConnManager(741): Make sure to release the connection before allocating another one.
03-23 16:32:25.822: D/dalvikvm(741): GC_CONCURRENT freed 40K, 3% free 9558K/9799K, paused 21ms+9ms
03-23 16:32:25.942: D/dalvikvm(741): GC_FOR_ALLOC freed 123K, 5% free 9629K/10119K, paused 68ms
03-23 16:32:25.952: I/dalvikvm-heap(741): Grow heap (frag case) to 9.770MB for 262160-byte allocation
03-23 16:32:26.092: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 6% free 9885K/10439K, paused 69ms
03-23 16:32:26.262: D/dalvikvm(741): GC_FOR_ALLOC freed 132K, 7% free 9757K/10439K, paused 55ms
03-23 16:32:26.262: I/dalvikvm-heap(741): Grow heap (frag case) to 10.145MB for 524304-byte allocation
03-23 16:32:26.412: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 7% free 10269K/11015K, paused 57ms
03-23 16:32:26.682: D/dalvikvm(741): GC_CONCURRENT freed 262K, 10% free 10014K/11015K, paused 13ms+3ms
03-23 16:32:26.852: D/dalvikvm(741): GC_FOR_ALLOC freed 1K, 10% free 10013K/11015K, paused 71ms
03-23 16:32:26.872: I/dalvikvm-heap(741): Grow heap (frag case) to 10.895MB for 1048592-byte allocation
03-23 16:32:27.042: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 9% free 11037K/12103K, paused 63ms
03-23 16:32:27.302: D/dalvikvm(741): GC_CONCURRENT freed 518K, 14% free 10526K/12103K, paused 26ms+5ms
03-23 16:32:28.212: D/dalvikvm(741): GC_FOR_ALLOC freed 9K, 14% free 10525K/12103K, paused 102ms
03-23 16:32:28.242: I/dalvikvm-heap(741): Grow heap (frag case) to 12.395MB for 2097168-byte allocation
03-23 16:32:28.562: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 12% free 12573K/14215K, paused 89ms
03-23 16:32:28.912: D/dalvikvm(741): GC_CONCURRENT freed 1030K, 19% free 11550K/14215K, paused 18ms+23ms
03-23 16:32:31.352: D/dalvikvm(741): GC_FOR_ALLOC freed 26K, 19% free 11549K/14215K, paused 79ms
03-23 16:32:31.412: I/dalvikvm-heap(741): Grow heap (frag case) to 15.395MB for 4194320-byte allocation
03-23 16:32:31.723: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 15% free 15645K/18375K, paused 87ms
03-23 16:32:31.932: D/dalvikvm(741): GC_CONCURRENT freed 2054K, 27% free 13598K/18375K, paused 19ms+4ms
03-23 16:32:38.491: D/dalvikvm(741): GC_FOR_ALLOC freed 59K, 27% free 13597K/18375K, paused 67ms
03-23 16:32:38.621: I/dalvikvm-heap(741): Grow heap (frag case) to 21.395MB for 8388624-byte allocation
03-23 16:32:39.031: D/dalvikvm(741): GC_FOR_ALLOC freed 0K, 19% free 21789K/26631K, paused 98ms
03-23 16:32:39.341: D/dalvikvm(741): GC_CONCURRENT freed 4101K, 34% free 17694K/26631K, paused 17ms+11ms
03-23 16:32:52.093: D/dalvikvm(741): GC_FOR_ALLOC freed 124K, 34% free 17693K/26631K, paused 79ms
03-23 16:32:52.093: I/dalvikvm-heap(741): Forcing collection of SoftReferences for 16777232-byte allocation
03-23 16:32:52.242: D/dalvikvm(741): GC_BEFORE_OOM freed 8K, 34% free 17685K/26631K, paused 142ms
03-23 16:32:52.242: E/dalvikvm-heap(741): Out of memory on a 16777232-byte allocation.
03-23 16:32:52.242: I/dalvikvm(741): "Thread-12" prio=5 tid=10 RUNNABLE
03-23 16:32:52.251: I/dalvikvm(741):   | group="main" sCount=0 dsCount=0 obj=0x408b9158 self=0x16f860
03-23 16:32:52.251: I/dalvikvm(741):   | sysTid=752 nice=0 sched=0/0 cgrp=default handle=1669016
03-23 16:32:52.251: I/dalvikvm(741):   | schedstat=( 5912317448 1115812658 845 ) utm=511 stm=80 core=0
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.util.CharArrayBuffer.expand(CharArrayBuffer.java:~59)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.util.CharArrayBuffer.append(CharArrayBuffer.java:77)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.util.EntityUtils.toString(EntityUtils.java:136)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:76)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
03-23 16:32:52.251: I/dalvikvm(741):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
03-23 16:32:52.262: I/dalvikvm(741):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
03-23 16:32:52.262: I/dalvikvm(741):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
03-23 16:32:52.262: I/dalvikvm(741):   at com.shr.khg.MainScreen.login(MainScreen.java:170)
03-23 16:32:52.262: I/dalvikvm(741):   at com.shr.khg.MainScreen.access$0(MainScreen.java:118)
03-23 16:32:52.262: I/dalvikvm(741):   at com.shr.khg.MainScreen$1.run(MainScreen.java:79)
03-23 16:32:55.752: W/dalvikvm(741): threadid=10: thread exiting with uncaught exception (group=0x40014760)
03-23 16:32:55.772: E/AndroidRuntime(741): FATAL EXCEPTION: Thread-12
03-23 16:32:55.772: E/AndroidRuntime(741): java.lang.OutOfMemoryError
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.util.CharArrayBuffer.expand(CharArrayBuffer.java:59)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.util.CharArrayBuffer.append(CharArrayBuffer.java:77)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.util.EntityUtils.toString(EntityUtils.java:136)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:76)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
03-23 16:32:55.772: E/AndroidRuntime(741):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
03-23 16:32:55.772: E/AndroidRuntime(741):  at com.shr.khg.MainScreen.login(MainScreen.java:170)
03-23 16:32:55.772: E/AndroidRuntime(741):  at com.shr.khg.MainScreen.access$0(MainScreen.java:118)
03-23 16:32:55.772: E/AndroidRuntime(741):  at com.shr.khg.MainScreen$1.run(MainScreen.java:79)
03-23 16:32:59.012: I/Process(741): Sending signal. PID: 741 SIG: 9

谢谢!

4

1 回答 1

1

这是一个众所周知的问题....首先您应该在 AsyncTask 中实现您的 http 请求....然后您就不会再看到这个错误了!!!!!!!!!!!!!!!

于 2013-03-23T16:47:17.297 回答