我想像队列一样一一上传文件。简而言之,假设有 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;
`