我想像android中的队列一样一个接一个地上传文件

我想像android中的队列一样一个接一个地上传文件,android,upload,Android,Upload,我想像队列一样一个接一个地上传文件。简言之,假设有10个文件,我想上传所有文件,它应该检查网络连接是否可用,如果不可用,那么这些文件应该进入队列,稍后将尝试上传。它将继续尝试,直到队列中的任何一个文件离开。请帮助我,我如何在android中实现这一点 这是我迄今为止尝试过的示例代码 final Thread thread = new Thread(){ public void run() { try { while(true

我想像队列一样一个接一个地上传文件。简言之,假设有10个文件,我想上传所有文件,它应该检查网络连接是否可用,如果不可用,那么这些文件应该进入队列,稍后将尝试上传。它将继续尝试,直到队列中的任何一个文件离开。请帮助我,我如何在android中实现这一点

这是我迄今为止尝试过的示例代码

final Thread thread = new Thread(){
        public void run() {
            try {
                while(true){
                    ThisCaching ic = new ThisCaching(getApplicationContext());
                    fileToUpload = ic.getDataFromCache("audiofiles", "SELECT * FROM audiofiles WHERE STATUS = '2' OR STATUS = '3' ORDER BY rowid DESC");
                    if(fileToUpload != null)
                    {
                        for (int i=0; i<fileToUpload.size(); i++) {
                            try {
                                System.out.println("Current position while uploading ::> " + i);
                                Thread.sleep(1000);
                                ConstantData.selectedFile = fileToUpload.get(i).toString();
                                ConstantData.position = i;
                                if(checkConnectivity()){
                                    new Uploading().execute();
                                    myApplication.getHttpClient().getConnectionManager().closeExpiredConnections();
                                }else{
                                    String[] status = {"STATUS"};
                                    String[] val = {"2"};
                                    try {
                                        SQLiteDatabase sd = ThisDataHelper.getInstance(getApplicationContext()).getDB();
                                        new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                }
                            } catch (Exception e) {
                                return;
                            }            
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }            
        }
    };


        String line = null;
        HttpClient httpClient = null;
        HttpContext localContext = null;
        HttpPost httpPost = null;
        MultipartEntity entity = null;
        HttpResponse response = null;
        HttpEntity httpEntity =null;
        SQLiteDatabase sd = ThisDataHelper.getInstance(getApplicationContext()).getDB();
        try {
            String strArray[]={"http://myurl.com","filename",ConstantData.selectedFile,"activityType",ConstantData.activityType,"patientId",ConstantData.patientId,"caseId",ConstantData.caseId,"doctorId",ConstantData.doctorId,"clinicCode",ConstantData.clinicCode,"documentType",ConstantData.documentType,"templateId",ConstantData.templateId};
            httpClient = new DefaultHttpClient();
            //httpClient.getConnectionManager().r
            localContext = new BasicHttpContext();
            httpPost = new HttpPost(strArray[0]);
            entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            File file = new File(ConstantData.selectedFile);
            String type = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")+1);
            if(strArray.length>1)
            {
                for(int i=1;i<strArray.length;i=i+2)
                {
                    if(strArray[i]!=null){
                        if(!strArray[i].equalsIgnoreCase("")){
                            //                              System.out.println("i) : "+i +"  i+1) : "+(i+1));
                            Log.i(strArray[i], "::" + strArray[i+1]);
                            if(strArray[i].equalsIgnoreCase("filename")){
                                entity.addPart("filename", new FileBody(file,"audio/"+type));
                            }else{
                                entity.addPart(strArray[i], new StringBody(strArray[i+1]));
                            }
                        }
                    }
                }
            }
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost,localContext);// i m getting error at this line

            System.out.println("Response code is ::> " +response.getStatusLine().getStatusCode());
            code = response.getStatusLine().getStatusCode();
            httpEntity = response.getEntity();
            line = EntityUtils.toString(httpEntity, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            code=100;
            e.printStackTrace();
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            ConstantData.uploaded = false;
            String[] status = {"STATUS"};
            String[] val = {"3"};
            try {
                new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (MalformedURLException e) {
            code=100;
            e.printStackTrace();
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            ConstantData.uploaded = false;
            String[] status = {"STATUS"};
            String[] val = {"3"};
            try {
                new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (IOException e) {
            code=100;
            e.printStackTrace();
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
            ConstantData.uploaded = false;
            String[] status = {"STATUS"};
            String[] val = {"3"};
            try {
                new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (Exception e) {
            code=100;
            e.printStackTrace();
            ConstantData.uploaded = false;
            String[] status = {"STATUS"};
            String[] val = {"3"};
            try {
                new ThisTable(sd, "audiofiles").updateRow(status,val, "FILENAME = " + "'"+ConstantData.selectedFile+"'");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }finally{
//  


try {
//                   System.out.println("&&&& while realeasing connection");
////                     if(httpClient != null){
////                         httpClient.getConnectionManager().releaseConnection((ManagedClientConnection) httpClient.getConnectionManager(), -1, TimeUnit.MINUTES);
////                     }
//                   if(httpPost.getEntity().getContent() != null){
//                       InputStream content = httpPost.getEntity().getContent();
//                       content.close(); 
//                   }
//              } catch (Exception e) {
//                  e.printStackTrace();
//              }
            }

            return line;


`
final Thread Thread=新线程(){
公开募捐{
试一试{
while(true){
ThisCaching ic=新建ThisCaching(getApplicationContext());
fileToUpload=ic.getDataFromCache(“音频文件”,“从音频文件中选择*,其中状态为'2'或状态为'3'按rowid DESC排序”);
if(fileToUpload!=null)
{
对于(int i=0;i1)
{

对于(inti=1;i,从我所看到的,您使用纯线程吗(还看不到编辑)。因为Java 1.5有一个用于队列和多线程处理的包,这使live变得更容易

对于队列,您可以使用
Executors.newSingleThreadExecutor()
创建一个单线程执行器。您可以得到一个线程池,其中只有一个线程按照您提交的顺序执行您提交的每个任务
提交(可运行)
。Java garanties表示,执行器始终有一个线程在队列上工作(如果线程死亡,Java会自动重新创建线程)

您提交的每个任务都实现一个
Runnable
。因此,要实现您描述的三次重试,您只需要一个实现
Runnable
接口的类,该类计算重试次数,如果上载不成功,它会增加计数器并重新提交自身

这是遗嘱执行人和遗嘱执行人服务