-2

My codes is below :

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);
                }

The above code works fine. Also

public void sentPhoto(int folderID, String imageName, String imgBinary,
        String folderName) {
    AppManager.getInstance(context).sendPhoto(folderID, imageName,
            imgBinary, folderName);

}

and finally

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;
            }
        }

    }

Here is my question, Suppose my image size 100. If I've uploaded part but not all of it, then how can I stop the ongoing image upload? How is this done?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
kablu
  • 629
  • 1
  • 7
  • 26

1 Answers1

0

With the HTTP API you're currently using, you can't.

Normally, you would need to create a flag, say, isCancelled alongside the logic that runs the request, call .interrupt() on the Thread running the read/write code from another thread (e.g. the UI thread), catch the InterruptedException , check the flag and shutdown everything if it's true.

But, AFAIK, the blocking methods provided by HttpsURLConnection are not interruptible.

Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59