我的代码如下:
if (imgLists.size() != 0) {
for (int i = 0; i < imgLists.size(); i++) {
Log.e(TAG, "foldername and image:"
+ imgList.get(0)._mFolderName + ", and :"
+ imgLists.get(i).toString());
File imgFile = new File(CGConstants.ASSET_FOLDER
+ imgList.get(0)._mFolderName, imgLists.get(i)
.toString());
String _mImageName = imgLists.get(i).toString();
Log.i(TAG, "imageName : in sendphoto" + _mImageName);
String _mImageBinary = AppManager.getInstance(context)
.imageToBinary(imgFile);
sentPhoto(
AppManager.getInstance(context).folderIDFromServer,
_mImageName, _mImageBinary,
imgList.get(0)._mFolderName);
}
上面的代码工作正常。 还
public void sentPhoto(int folderID, String imageName, String imgBinary,
String folderName) {
AppManager.getInstance(context).sendPhoto(folderID, imageName,
imgBinary, folderName);
}
最后
public void sendPhoto(int selectFolderId, String photoName,
String photoBinary, String folderName) {
HttpURLConnection http = null;
URL url;
try {
JSONObject json = new JSONObject();
json.put("session", mAuthKey);
json.put("folderid", selectFolderId);
json.put("photoname", photoName);
json.put("photobinary", photoBinary);
Log.e(TAG, "sendPhoto : folderid:" + selectFolderId);
Log.e(TAG, "sendPhoto : photoname:" + photoName);
Log.e(TAG, "sendPhoto : photobinary:" + photoBinary);
String postParameters = json.toString();
Log.e(TAG, "sendPhoto:" + "postParameters=" + postParameters);
url = new URL(URLPREFIX + "sendphoto");
Log.e(TAG, "sendPhoto url :" + url.toString());
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url
.openConnection();
https.setDoOutput(true);
https.setRequestProperty("Content-Type", "text/plain");
// https.setChunkedStreamingMode(0); // Use default chunk size
https.setHostnameVerifier(DO_NOT_VERIFY);
https.setRequestMethod("POST");
https.connect();
// http = https;
// Write serialized JSON data to output stream.
OutputStream out = new BufferedOutputStream(https.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
out, "UTF-8"));
writer.write(postParameters.toString());
// Close streams and disconnect.
writer.close();
out.close();
int code = https.getResponseCode();
Log.e(TAG, "sendPhoto : code:" + code);
http = https;
reader = new BufferedReader(new InputStreamReader(
https.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
JSONObject jsonObject = new JSONObject(sb.toString());
Log.e(TAG, "sendPhoto :jsonObject :" + jsonObject.toString());
mSuccess = jsonObject.getInt("Status");
Log.e(TAG, "sendPhoto : mSuccess: " + mSuccess);
String status = "";
if (mSuccess == 1) {
// // UPDATE IMAGE STATUS IN DB
// boolean updateStatus = updateImgStatus(photoName, folderName,
// "1");
// if (updateStatus) {
// Log.e(TAG, "sendPhoto : mSuccess : updateStatus:image :"
// + photoName + " is sychronized");
// } else {
// Log.e(TAG, "sendPhoto : mSuccess : updateStatus:image :"
// + photoName + " is sychronized fails");
// }
// Log.e(TAG, "sendPhoto : mSuccess :1");
status = endSync(selectFolderId, folderName);
} else if (mSuccess == 0) {
} else if (mSuccess == -1) {
} else {
//
}
} catch (MalformedURLException e) {
Log.e(TAG, "sendPhoto : MalformedURLException" + e.toString());
e.printStackTrace();
} catch (JSONException e) {
Log.e(TAG, "sendPhoto : JSONException" + e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, "sendPhoto : IOException" + e.toString());
e.printStackTrace();
} finally {
try {
reader.close();
}
catch (Exception ex) {
mSuccess = 0;
}
}
}
这是我的问题,假设我的图片大小为100.如果我上传了部分但不是全部,那么我该如何停止正在进行的图片上传?这是怎么做到的?
答案 0 :(得分:0)
使用您当前使用的HTTP API,您不能。
通常,您需要在运行请求的逻辑旁边创建一个标志,例如isCancelled
,在另一个线程上运行读/写代码的.interrupt()
上调用Thread
(例如UI线程),捕获InterruptedException
,检查标志并关闭所有内容(如果它是true
。
但是,AFAIK,HttpsURLConnection
提供的阻止方法不可中断。