我正在尝试将图像上传到远程tomcat服务器。但是我得到了错误

时间:2019-02-11 10:15:32

标签: android image upload

我正在尝试将图像从android上传到远程tomcat服务器。 但是我收到此错误“错误:方法未从超类型重写或实现方法” ,并且不确定为什么会这样。我使用了http://programmerguru.com/android-tutorial/how-to-upload-image-to-java-服务器/教程作为参考。

@SuppressLint("NewApi")
        public class FileUploadActivity extends Activity {
        ProgressDialog prgDialog;
        String encodedString;
        RequestParams params = new RequestParams();
        String imgPath, fileName;
        Bitmap bitmap;
        private static int RESULT_LOAD_IMG = 1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_file_upload);
            prgDialog = new ProgressDialog(this);
            // Set Cancelable as False
            prgDialog.setCancelable(false);
        }

        public void loadImagefromGallery(View view) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,

            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            try {
                if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {

                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imgPath = cursor.getString(columnIndex);
                    cursor.close();
                    ImageView imgView = (ImageView) findViewById(R.id.imgView);
                    imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgPath));
                    String fileNameSegments[] = imgPath.split("/");
                    fileName = fileNameSegments[fileNameSegments.length - 1];
                    params.put("filename", fileName);

                } else {
                    Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }

        public void uploadImage(View v) {
            if (imgPath != null && !imgPath.isEmpty()) {
                prgDialog.setMessage("Converting Image to Binary Data");
                prgDialog.show();
                encodeImagetoString();
            } else {
                Toast.makeText( getApplicationContext(), "You must select image from gallery before you try to upload",Toast.LENGTH_LONG).show();
            }
        }

        public void encodeImagetoString() {
            new AsyncTask<Void, Void, String>() {

                protected void onPreExecute() {};

                @Override
                protected String doInBackground(Void... params) {
                    BitmapFactory.Options options = null;
                    options = new BitmapFactory.Options();
                    options.inSampleSize = 3;
                    bitmap = BitmapFactory.decodeFile(imgPath, options);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
                    byte[] byte_arr = stream.toByteArray();
                    encodedString = Base64.encodeToString(byte_arr, 0);
                    return "";
                }

                @Override
                protected void onPostExecute(String msg) {
                    prgDialog.setMessage("Calling Upload");
                    params.put("image", encodedString);
                    triggerImageUpload();
                }
            }.execute(null, null, null);
        }

        public void triggerImageUpload() {
            makeHTTPCall();
        }

        public void makeHTTPCall() {
            prgDialog.setMessage("Invoking JSP");
            AsyncHttpClient client = new AsyncHttpClient();
            client.post("http://myURL:8080/webservice/uploadimg.jsp" ,
                params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, 
                    byte[] responseBody) {
                        prgDialog.hide();
                        Toast.makeText(getApplicationContext(), 
                        responseBody.toString(),
                        Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, 
                    byte[] responseBody, Throwable error) {
                        prgDialog.hide();
                        if (statusCode == 404) {
                            Toast.makeText(getApplicationContext(),
                            "Requested resource not found",
                            Toast.LENGTH_LONG).show();
                        }
                        else if (statusCode == 500) {
                            Toast.makeText(getApplicationContext(),
                            "Something went wrong at server end",
                            Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(getApplicationContext(),"Error Occured n Most Common Error: n1.Device not connected to Internetn2. Web App is not deployed in App servern3. App server is not runningn HTTP Status code : " + statusCode, Toast.LENGTH_LONG).show();
                        }
                    }
                }
            );
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (prgDialog != null) {
                prgDialog.dismiss();
            }
        }
    }

这些是我的gradle依赖项:

dependencies {
implementation 'com.android.support:multidex:1.0.3'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso- 
core:3.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:leanback-v17:27.1.1'
implementation 'de.hdodenhof:circleimageview:2.1.0'
//  libraries for web service --->
//    compile 'org.glassfish.jersey.containers:jersey-container- 
servlet:2.14'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
//    <---
//Add Swipe card library
implementation 'com.huxq17.android:SwipeCardsView:1.3.5'
implementation 'com.squareup.picasso:picasso:2.5.2'
compile 'com.loopj.android:android-async-http:1.4.9'

//    compile files('libs/activation.jar')
//    compile files('libs/additionnal.jar')
//    compile files('libs/mail.jar')
implementation files('libs/activation.jar')
implementation files('libs/additionnal.jar')
implementation files('libs/mail.jar')
implementation files('libs/android-async-http-1.4.4.jar')

1 个答案:

答案 0 :(得分:0)

我自己解决了此编译错误。从gradle中删除了编译'com.loopj.android:android-async-http:1.4.9'并将AsyncHttpResponseHandler更改为//响应代码'200'             @Override             public void onSuccess(String response){                 //隐藏进度对话框                 prgDialog.hide();                 Toast.makeText(getApplicationContext(),响应,                         Toast.LENGTH_LONG).show();             }

        // When the response returned by REST has Http
        // response code other than '200' such as '404',
        // '500' or '403' etc
        @Override
        public void onFailure(int statusCode, Throwable error,
                              String content) {
            // Hide Progress Dialog
            prgDialog.hide();
            // When Http response code is '404'
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(),
                        "Requested resource not found",
                        Toast.LENGTH_LONG).show();
            }
            // When Http response code is '500'
            else if (statusCode == 500) {
                Toast.makeText(getApplicationContext(),
                        "Something went wrong at server end",
                        Toast.LENGTH_LONG).show();
            }
            // When Http response code other than 404, 500
            else {
                Toast.makeText(
                        getApplicationContext(),
                        "Error Occured n Most Common Error: n1. Device not connected to Internetn2. Web App is not deployed in App servern3. App server is not runningn HTTP Status code : "
                                + statusCode, Toast.LENGTH_LONG)
                        .show();
            }
        }