Skip to content

always executed onFailure when file uploading #356

Closed
@sergey-var

Description

@sergey-var
public void uploadFile(final AdapterData data, final int positionThread) {

        final boolean[] fail = {true};
        Photo photo = data.getPhoto();
        String sourceFileUri = photo.getImagePath();
        File sourceFile = new File(sourceFileUri);
        if (!sourceFile.isFile()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity, getString(R.string.file_not_found), Toast.LENGTH_SHORT).show();
                }
            });
            kickFromStack(data);
            return;
        }
        RequestParams params = null;
        final AsyncHttpClient client = new AsyncHttpClient();
        try {
            params = new RequestParams();
            params.put("file", sourceFile);
            params.put("date_time", String.valueOf(photo.getDateTime()));
            params.put("image_name", photo.getImageName());
            params.put("image_type", String.valueOf(photo.getImageType()));
            params.put("latitude", String.valueOf(photo.getGpsLatitudeCoordinate()));
            params.put("longtitude", String.valueOf(photo.getGpsLongitudeCoordinate()));
            params.put("altitude", String.valueOf(photo.getAltitude()));
            params.put("image_description", String.valueOf(photo.getImageDescription()));
        } catch(FileNotFoundException e) {
            e.printStackTrace();
            kickFromStack(data);
        }

        AsyncHttpResponseHandler progressHandler = new AsyncHttpResponseHandler(){
            int process = 0;
            @Override
            public void onProgress(int position, int length) {
                super.onProgress(position, length);
                if(stackUploadingToCancel.size() > 0){
                    ArrayList<AdapterData> tempList = new ArrayList<AdapterData>();
                    tempList.addAll(stackUploadingToCancel);
                    for(AdapterData cancelData : tempList){
                        if(cancelData.getId() == positionThread){
                            notificationUtils.showCancelNotification(positionThread);
                            stackUploadingToCancel.remove(cancelData);
                            client.getHttpClient().getConnectionManager().shutdown();
                            fail[0] = false;
                            return;
                        }
                    }
                }
                int percent = length / 100;
                int progress = position / percent;
                if(process != progress){
                    notificationUtils.updateNotification(positionThread, progress);
                    updateStatus(positionThread, progress);
                    process = progress;
                }
            }

            /**
             * Fired when a request returns successfully, override to handle in your own code
             *
             * @param statusCode   the status code of the response
             * @param headers      return headers, if any
             * @param responseBody the body of the HTTP response from the server
             */
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                super.onSuccess(statusCode, headers, responseBody);
                try {
                    JSONObject jsonObject = new JSONObject(new String(responseBody, "UTF-8"));
                    String id = jsonObject.getString("server_id");
                    onProcessExecuted(data, positionThread, id);
                    Log.e("resp", id);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
                notificationUtils.initDownloadNotification(positionThread);
            }

            /**
             * Fired when a request fails to complete, override to handle in your own code
             *
             * @param statusCode   return HTTP status code
             * @param headers      return headers, if any
             * @param responseBody the response body, if any
             * @param error        the underlying cause of the failure
             */
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                super.onFailure(statusCode, headers, responseBody, error);
                if(fail[0]){
                    notificationUtils.showFailNotification(positionThread);
                    data.setStatus(StatusUploading.FAIL);
                }else {
                    failUpload(data, error.toString());
                    UploadingProcess.stackUploading.replace(data, positionThread);
                }
                if(UploadingProcess.adapter != null){
                    updateAdapter();
                }
            }
        };
        client.post(upLoadServerUri, params, progressHandler);
    }

When i'm trying upload image to server, always executed onFailure method with:

headers == null;
statusCode == 0
error == java.io.IOException: Unhandled exception: divide by zero
responseBody == null

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions