我想像队列一样逐个上传文件。简而言之,假设有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;
`
答案 0 :(得分:0)
从我看到的,您使用普通线程(无法看到编辑)。从Java 1.5开始,有一个包括队列和多线程的包,这使得生活变得更加容易。
出于队列的目的,您可以使用Executors.newSingleThreadExecutor()
创建单个线程执行程序。你得到的Threadpool只有一个线程,按你提交的顺序执行submit(Runnable)
的每个任务。 Java garanties,Executor总是有一个线程处理队列(如果它死了,Java会自动重新创建线程)
您提交的每项任务都会实现Runnable
。因此,要实现您描述的三个重试,您只需要一个实现Runnable
接口的类,它计算重试次数,如果上传不成功,它会递增计数器并重新提交。