我制作了一个简单的应用程序,它只是从互联网上下载一些数据,然后不断处理它。但是我发现在屏幕关闭后半秒或一段时间后应用程序停止工作。并且应用程序没有错误。似乎应用程序重新启动,一切正常,只是没有下载数据。这是我的代码。
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... url) {
// constants
int timeoutSocket = 5000;
int timeoutConnection = 5000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
System.out.println(GlobalVariables.getRandomString(15));
HttpGet httpget = new HttpGet(url[0] + "?id=" + GlobalVariables.getRandomString(15));
try {
HttpResponse getResponse = client.execute(httpget);
getResponse.addHeader("Cache-Control:", "no-cache");
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
return null;
}
String line = "";
StringBuilder total = new StringBuilder();
HttpEntity getResponseEntity = getResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));
while((line = reader.readLine()) != null) {
total.append(line);
total.append("\n");
}
line = total.toString();
return line;
} catch (Exception e) {
Log.w("MyApp", "Download Exception : " + e.toString());
}
return null;
}
@Override
protected void onPreExecute() {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: Downloading Data, Please Wait....");
}
@Override
protected void onPostExecute(String result) {
try {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: Downloading Complete.");
if(result.toString().length() > 10) {
errorcount = 0;
MyMessageDetails Detail;
String[] mydata = result.split("\\|s\\|");
for (String value : mydata)
{
if (value.contains("|")){
String[] details = value.split("\\|n\\|");
String mymsg = null;
Detail = new MyMessageDetails();
Detail.setIcon(R.drawable.ic_launcher);
for (String mydetail : details)
{
if (mydetail.startsWith("_From:_")){
mymsg = "From: " + mydetail.replace("_From:_", "")+ "\n";
Detail.setMsgBody(mymsg);
}
}
titles.add(Detail);
}
}
if (titles.size() > 0){
ListView listView1 = (ListView)findViewById(R.id.lstmsgs);
ca = new CustomAdapter(titles , MainActivity.this);
listView1.setAdapter(ca);
}else{
if (StartInfo){
new RequestTask().execute(BaseUrl);
}
}
}else{
if (StartInfo){
new RequestTask().execute(BaseUrl);
}
}
} catch(NullPointerException e) {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: " + "Error: " + e.getCause());
}
}
更新 LogCat看起来像这样。
09-08 22:16:32.650: D/jdwp(9057): sendBufferedRequest : len=0x3F
09-08 22:16:32.713: D/ActivityThread(9057): BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{417d01d0 com.golden.smsserver}}
09-08 22:16:32.788: D/ThemeManager(9057): contextandroid.app.ContextImpl@417d2460
09-08 22:16:32.788: D/ThemeManager(9057): create ThemeManager object
09-08 22:16:32.789: D/ThemeManager(9057): Res_clear()
09-08 22:16:32.806: D/ThemeManager(9057): packageName=====com.golden.smsserver
09-08 22:16:32.809: D/ThemeManager(9057): packageName=com.golden.smsserver
09-08 22:16:32.810: V/Provider/Setting(9057): invalidate [system]: current 25 != cached 0
09-08 22:16:32.813: D/RRR(9057): path=other/bottom/bottom7.png
09-08 22:16:32.872: D/ThemeManager(9057): sIconWidth=64----sIconHeight=64
09-08 22:16:32.875: D/ThemeManagerHH(9057): infoDrawable=72
09-08 22:16:33.000: D/dalvikvm(9057): threadid=11: interp stack at 0x4c0b0000
09-08 22:16:33.000: D/dalvikvm(9057): threadid=11: calling run()
09-08 22:16:33.007: D/dalvikvm(9057): threadid=11: exiting
09-08 22:16:33.008: D/dalvikvm(9057): threadid=11: bye!
09-08 22:16:33.008: D/dalvikvm(9057): threadid=0: freeing
09-08 22:16:33.009: D/ActivityThread(9057): ACT-AM_ON_RESUME_CALLED ActivityRecord{417d1800 token=android.os.BinderProxy@417d1038 {com.golden.smsserver/com.golden.smsserver.MainActivity}}
09-08 22:16:33.029: D/ActivityThread(9057): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{417d1800 token=android.os.BinderProxy@417d1038 {com.golden.smsserver/com.golden.smsserver.MainActivity}}
09-08 22:16:33.117: D/libEGL(9057): loaded /system/lib/egl/libGLES_android.so
09-08 22:16:33.126: D/libEGL(9057): loaded /vendor/lib/egl/libEGL_mtk.so
09-08 22:16:33.147: D/libEGL(9057): loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
09-08 22:16:33.156: D/libEGL(9057): loaded /vendor/lib/egl/libGLESv2_mtk.so
09-08 22:16:33.243: D/OpenGLRenderer(9057): Enabling debug mode 0
09-08 22:16:36.226: D/dalvikvm(9057): threadid=11: interp stack at 0x4e02e000
09-08 22:16:36.226: D/dalvikvm(9057): threadid=11: calling run()
09-08 22:16:36.229: I/System.out(9057): ns7iqohduryaote
09-08 22:16:36.317: I/System.out(9057): [socket][0] connection /192.168.10.9:8080;LocalPort=33406(5000)
09-08 22:16:36.317: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:36.328: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:36.424: I/System.out(9057): [socket][/192.168.104.101:33406]
09-08 22:16:36.425: I/System.out(9057): rx timeout:5000
09-08 22:16:37.273: D/dalvikvm(9057): threadid=12: interp stack at 0x4e082000
09-08 22:16:37.273: D/dalvikvm(9057): threadid=12: calling run()
09-08 22:16:37.273: I/System.out(9057): 6bui5dlvbf445wr
09-08 22:16:37.299: I/System.out(9057): [socket][1] connection /192.168.10.9:8080;LocalPort=34375(5000)
09-08 22:16:37.300: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:37.300: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:40.457: I/System.out(9057): [socket][/192.168.104.101:34375]
09-08 22:16:40.457: I/System.out(9057): rx timeout:5000
09-08 22:16:41.496: I/System.out(9057): rx timeout:10
09-08 22:16:41.496: I/System.out(9057): rx timeout:5000
09-08 22:16:41.497: I/System.out(9057): rx timeout:10
09-08 22:16:41.497: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:41.497: I/System.out(9057): rx timeout:5000
09-08 22:16:41.497: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:41.499: I/System.out(9057): [CDS]close[34375]
09-08 22:16:41.501: I/System.out(9057): close [socket][/0.0.0.0:34375]
09-08 22:16:41.502: I/System.out(9057): close [socket][/0.0.0.0:34375]
09-08 22:16:41.513: D/dalvikvm(9057): threadid=13: interp stack at 0x4e223000
09-08 22:16:41.513: D/dalvikvm(9057): threadid=13: calling run()
09-08 22:16:41.514: I/System.out(9057): urz5mb57hes3124
09-08 22:16:41.549: I/System.out(9057): [socket][2] connection /192.168.10.9:8080;LocalPort=36147(5000)
09-08 22:16:41.549: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:41.550: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:41.640: I/System.out(9057): [socket][/192.168.104.101:36147]
09-08 22:16:41.640: I/System.out(9057): rx timeout:5000
09-08 22:16:42.206: I/System.out(9057): rx timeout:10
09-08 22:16:42.210: I/System.out(9057): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
09-08 22:16:42.210: I/System.out(9057): [CDS]read is 0
09-08 22:16:42.211: I/System.out(9057): rx timeout:5000
09-08 22:16:42.211: I/System.out(9057): rx timeout:10
09-08 22:16:42.219: I/System.out(9057): rx timeout:5000
09-08 22:16:42.219: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:42.219: I/System.out(9057): [CDS]close[36147]
09-08 22:16:42.220: I/System.out(9057): close [socket][/0.0.0.0:36147]
09-08 22:16:42.220: I/System.out(9057): close [socket][/0.0.0.0:36147]
09-08 22:16:42.229: D/dalvikvm(9057): threadid=14: interp stack at 0x4e22b000
09-08 22:16:42.229: D/dalvikvm(9057): threadid=14: calling run()
09-08 22:16:42.230: I/System.out(9057): whm0kske88z9hwr
09-08 22:16:42.233: I/System.out(9057): [socket][3] connection /192.168.10.9:8080;LocalPort=57208(5000)
09-08 22:16:42.234: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:42.235: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:42.238: I/System.out(9057): [socket][/192.168.104.101:57208]
09-08 22:16:42.238: I/System.out(9057): rx timeout:5000
09-08 22:16:42.759: I/System.out(9057): rx timeout:10
09-08 22:16:42.760: I/System.out(9057): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
09-08 22:16:42.760: I/System.out(9057): [CDS]read is 0
09-08 22:16:42.760: I/System.out(9057): rx timeout:5000
09-08 22:16:42.760: I/System.out(9057): rx timeout:10
09-08 22:16:42.761: I/System.out(9057): rx timeout:5000
09-08 22:16:42.761: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:42.761: I/System.out(9057): [CDS]close[57208]
09-08 22:16:42.762: I/System.out(9057): close [socket][/0.0.0.0:57208]
09-08 22:16:42.762: I/System.out(9057): close [socket][/0.0.0.0:57208]
09-08 22:16:42.763: D/dalvikvm(9057): threadid=15: interp stack at 0x4e233000
09-08 22:16:42.764: D/dalvikvm(9057): threadid=15: calling run()
09-08 22:16:42.765: I/System.out(9057): 769h9vhnoeafrbi
09-08 22:16:42.768: I/System.out(9057): [socket][4] connection /192.168.10.9:8080;LocalPort=60430(5000)
09-08 22:16:42.770: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:42.773: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:42.775: I/System.out(9057): [socket][/192.168.104.101:60430]
09-08 22:16:42.775: I/System.out(9057): rx timeout:5000
09-08 22:16:43.335: I/System.out(9057): rx timeout:10
09-08 22:16:43.340: I/System.out(9057): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
09-08 22:16:43.340: I/System.out(9057): [CDS]read is 0
09-08 22:16:43.341: I/System.out(9057): rx timeout:5000
09-08 22:16:43.341: I/System.out(9057): rx timeout:10
09-08 22:16:43.347: I/System.out(9057): rx timeout:5000
09-08 22:16:43.347: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:43.347: I/System.out(9057): [CDS]close[60430]
09-08 22:16:43.348: I/System.out(9057): close [socket][/0.0.0.0:60430]
09-08 22:16:43.348: I/System.out(9057): close [socket][/0.0.0.0:60430]
09-08 22:16:43.349: I/System.out(9057): 4fm3442av72z63k
09-08 22:16:43.355: I/System.out(9057): [socket][5] connection /192.168.10.9:8080;LocalPort=41743(5000)
09-08 22:16:43.355: I/System.out(9057): [CDS]connect[/192.168.10.9:8080] tm:5
09-08 22:16:43.356: D/Posix(9057): [Posix_connect Debug]Process com.golden.smsserver :8080
09-08 22:16:43.359: I/System.out(9057): [socket][/192.168.104.101:41743]
09-08 22:16:43.360: I/System.out(9057): rx timeout:5000
09-08 22:16:44.000: I/System.out(9057): rx timeout:10
09-08 22:16:44.010: I/System.out(9057): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
09-08 22:16:44.010: I/System.out(9057): [CDS]read is 0
09-08 22:16:44.010: I/System.out(9057): rx timeout:5000
09-08 22:16:44.011: I/System.out(9057): rx timeout:10
09-08 22:16:44.012: I/System.out(9057): rx timeout:5000
09-08 22:16:44.012: I/System.out(9057): [CDS]shutdownInput
09-08 22:16:44.012: I/System.out(9057): [CDS]close[41743]
09-08 22:16:44.013: I/System.out(9057): close [socket][/0.0.0.0:41743]
09-08 22:16:44.013: I/System.out(9057): close [socket][/0.0.0.0:41743]
09-08 22:16:44.015: I/System.out(9057): z56eof2knllxpm3
更新2
public class MainActivity extends Activity implements android.view.View.OnClickListener{
public boolean StartInfo;
CustomAdapter ca;
ArrayList<MyMessageDetails> titles = new ArrayList<MyMessageDetails>();
String BaseUrl = My URL
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnsend = (Button) findViewById(R.id.btnsend);
btnsend.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.btnsend:
Button btnsend = (Button) findViewById(R.id.btnsend);
EditText etcounter = (EditText) findViewById(R.id.etcounter);
if (StartInfo){
btnsend.setText("Start");
etcounter.setEnabled(true);
StartInfo = false;
}else{
btnsend.setText("Stop");
etcounter.setEnabled(true);
StartInfo = true;
new RequestTask().execute(BaseUrl);
}
break;
}
}
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... url) {
// constants
int timeoutSocket = 5000;
int timeoutConnection = 5000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
System.out.println(GlobalVariables.getRandomString(15));
HttpGet httpget = new HttpGet(url[0] + "?id=" + GlobalVariables.getRandomString(15));
try {
HttpResponse getResponse = client.execute(httpget);
getResponse.addHeader("Cache-Control:", "no-cache");
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
return null;
}
String line = "";
StringBuilder total = new StringBuilder();
HttpEntity getResponseEntity = getResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));
while((line = reader.readLine()) != null) {
total.append(line);
total.append("\n");
}
line = total.toString();
return line;
} catch (Exception e) {
Log.w("MyApp", "Download Exception : " + e.toString());
}
return null;
}
@Override
protected void onPreExecute() {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: Downloading Data, Please Wait....");
}
@Override
protected void onPostExecute(String result) {
try {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: Downloading Complete.");
if(result.toString().length() > 10) {
MyMessageDetails Detail;
String[] mydata = result.split("\\|s\\|");
for (String value : mydata)
{
if (value.contains("|")){
String[] details = value.split("\\|n\\|");
String mymsg = null;
Detail = new MyMessageDetails();
Detail.setIcon(R.drawable.ic_launcher);
for (String mydetail : details)
{
if (mydetail.startsWith("_From:_")){
mymsg = "From: " + mydetail.replace("_From:_", "")+ "\n";
Detail.setMsgBody(mymsg);
}
}
titles.add(Detail);
}
}
if (titles.size() > 0){
ListView listView1 = (ListView)findViewById(R.id.lstmsgs);
ca = new CustomAdapter(titles , MainActivity.this);
listView1.setAdapter(ca);
}else{
if (StartInfo){
new RequestTask().execute(BaseUrl);
}
}
}else{
if (StartInfo){
new RequestTask().execute(BaseUrl);
}
}
} catch(NullPointerException e) {
TextView txtstatus = (TextView) findViewById(R.id.txtstatus);
txtstatus.setText("Status: " + "Error: " + e.getCause());
}
}
}