Android AsyncTask在一段时间后没有响应

时间:2014-05-07 06:51:38

标签: android android-asynctask android-service

大家好我有一个AsyncTask类

public class PostFileProc extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... params) {

    fileName1 = "somefile";


    try {

        FileInputStream fileInputStream = new FileInputStream(new File(
                fileName1));

        URL url = new URL(urlPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("ENCTYPE", "multipart/form-data");
        connection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream
                .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                        + fileName1 + "\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        serverResponseMessage = connection.getResponseMessage();

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();

    }

    catch (Exception ex) {
        ex.getMessage();
    }


    return null;
}

我在呼叫广播或传入短信广播的每个听众的服务中将此类称为

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
PostFileProc upFile = new PostFileProc();
                upFile.execute();
}

然后它会在来电和来电短信上成功运行9-10次,但之后我的服务就会停止。程序将不再响应。 请有人帮帮我,是吗?

0 个答案:

没有答案