在文件上传到服务器的Andr​​oid月食错误月食、文件上传、错误、服务器

2023-09-05 01:36:59 作者:我存在你的存在

在我的Andr​​oid Eclipse项目,我要上传的图像文件名和电子邮件领域的服务器。

但我得到了以下错误:我logcat的是以下内容:

  NoSuchFieldError  -  BasicHeaderValueFormatter.INSTANCE

E / AndroidRuntime(25348):java.lang.NoSuchFieldError:产生的原因org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
 

我的整个logcat的:

  E / AndroidRuntime(25348):致命异常:AsyncTask的#1
E / AndroidRuntime(25348):工艺:com.example.uploadfiles,PID:25348
E / AndroidRuntime(25348):java.lang.RuntimeException的:执行doInBackground时出错()
E / AndroidRuntime(25348):在android.os.AsyncTask $ 3.done(AsyncTask.java:300)
E / AndroidRuntime(25348):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E / AndroidRuntime(25348):在java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E / AndroidRuntime(25348):在java.util.concurrent.FutureTask.run(FutureTask.java:242)
E / AndroidRuntime(25348):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:231)
E / AndroidRuntime(25348):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E / AndroidRuntime(25348):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
E / AndroidRuntime(25348):在java.lang.Thread.run(Thread.java:841)
E / AndroidRuntime(25348):java.lang.NoSuchFieldError:产生的原因org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
E / AndroidRuntime(25348):在org.apache.http.entity.ContentType.toString(ContentType.java:153)
E / AndroidRuntime(25348):在org.apache.http.entity.mime.MultipartFormEntity< INIT>(MultipartFormEntity.java:52)。
E / AndroidRuntime(25348):在org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
E / AndroidRuntime(25348):在org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
E / AndroidRuntime(25348):在com.example.uploadfiles.MainActivity $ ImageUploadTask.doInBackground(MainActivity.java:172)
E / AndroidRuntime(25348):在com.example.uploadfiles.MainActivity $ ImageUploadTask.doInBackground(MainActivity.java:1)
E / AndroidRuntime(25348):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
E / AndroidRuntime(25348):在java.util.concurrent.FutureTask.run(FutureTask.java:237)
E / AndroidRuntime(25348):4 ...更多
 

我的code上传图像和一些字段如下:

 类ImageUploadTask扩展的AsyncTask<虚空,虚空,字符串> {
    @燮pressWarnings(德precation)
    @覆盖
    保护字符串doInBackground(空... unsued){
        尝试 {

            文件图像=新的文件(IPATH);
            FileBody fileBody =新FileBody(形象);

            HttpClient的客户端=新DefaultHttpClient();
            HttpContext的localContext =新BasicHttpContext();
            HttpPost后=新HttpPost(Constant.signUp);

            post.setHeader(加密类型,多部分/表单数据);

            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            multipartEntity.addPart(ID,新StringBody(1));
            multipartEntity.addPart(PID,新StringBody(1));
            multipartEntity.addPart(userPhoto,fileBody);
            multipartEntity.addPart(电子邮件,新StringBody(joseph@gmail.com));
            multipartEntity.addPart(细胞,新StringBody(1234567890));
            multipartEntity.addPart(用户名,新StringBody(约瑟夫));

            post.setEntity(multipartEntity.build());
            // post.setEntity((MultipartEntityBuilder)multipartEntity);
            HTT presponse响应= client.execute(后,localContext);
            的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()
                    UTF-8));

            HTT presponse响应= client.execute(后);
            串responseBody = EntityUtils.toString(response.getEntity());
            Log.v(multiPartPost HTTP响应,responseBody);

        }赶上(例外五){

            的System.out.println(错误=+ e.getMessage());
            返回null;
        }
        返回 ;

    }

    @覆盖
    保护无效onProgressUpdate(空... unsued){

    }

    @覆盖
    保护无效onPostExecute(字符串sResponse){
        尝试 {

            如果(sResponse!= NULL){
                JSONObject的JResponse =新的JSONObject(sResponse);
                INT成功= JResponse.getInt(成功);
                字符串消息= JResponse.getString(消息);
                如果(成功== 0){
                    Toast.makeText(getApplicationContext(),消息,Toast.LENGTH_LONG).show();
                } 其他 {
                    Toast.makeText(getApplicationContext(),照片上传成功,Toast.LENGTH_SHORT)
                            。显示();
                    caption.setText();
                }
            }
        }赶上(例外五){
            Toast.makeText(getApplicationContext(),excepttion,Toast.LENGTH_LONG).show();
            Log.e(e.getClass()的getName(),e.​​getMessage(),E。);
        }
    }
}
 

我的libs文件夹有以下必要的库:

和我的Java构建路径:

而为了出口的性质

现在我什么都做,以解决这个错误:我工作一整天来解决这一点,但没有,所以请任何一个建议我如何解决这个错误,很美联社preciated,谢谢 解决方案

这是库的主要问题,所有可能面临的,错误的库导致此问题,

因此​​,接下来的步骤对您有帮助:

(1)code:遵照code成功上传照片到服务器:

  btnUpload.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            如果(Constant.bitmapPicture!= NULL){
                StoreByteImage(Constant.bitmapPicture,90,my_image);

            } 其他 {
                Toast.makeText(getApplicationContext(),图像空,Toast.LENGTH_LONG).show();
            }
        }
    });
 

StoreByteImage(...):

 公共布尔StoreByteImage(位图位图,诠释品质,串expName){

    FileOutputStream中的FileOutputStream = NULL;
    。字符串extStorageDirectory = Environment.getExternalStorageDirectory()的toString();
    文件myNewFolder =新的文件(extStorageDirectory +/例);
    如果(myNewFolder.mkdirs()){
        myNewFolder.mkdir();
    }
    尝试 {


        IPATH = myNewFolder +/+ expName +.JPG;
        的FileOutputStream =新的FileOutputStream(myNewFolder +/+ expName +.JPG);
        的BufferedOutputStream BOS =新的BufferedOutputStream(FileOutputStream中);
        bitmap.com preSS(比较pressFormat.JPEG,质量,BOS);

        新ImageUploadTask()执行()。

        bos.flush();
        bos.close();

    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    返回true;
}
 

类ImageUploadTask

 类ImageUploadTask扩展的AsyncTask<虚空,虚空,字符串> {

    字符串sResponse;

    @燮pressWarnings(德precation)
    @覆盖
    保护字符串doInBackground(空... unsued){
        尝试 {
            //

            文件图像=新的文件(IPATH);
            FileBody fileBody =新FileBody(形象);

            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpContext的localContext =新BasicHttpContext();
            HttpPost httpPost =新HttpPost(YourImageUploadURLHere);

            MultipartEntity实体=新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream BOS =新ByteArrayOutputStream();
            Constant.bitmapPicture.com preSS(比较pressFormat.JPEG,100,BOS);
            byte []的数据= bos.toByteArray();

            entity.addPart(ID,新StringBody(1));
            entity.addPart(PID,新StringBody(1));
            entity.addPart(userPhoto,fileBody);
            entity.addPart(电子邮件,新StringBody(email@gmail.com));
            entity.addPart(细胞,新StringBody(1234567890));
            entity.addPart(用户名,新StringBody(名字));

            httpPost.setEntity(实体);
            HTT presponse响应= httpClient.execute(httpPost,localContext);
            的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()
                    UTF-8));

            sResponse = reader.readLine();
            的System.out.println(性反应=+ sResponse);

        }赶上(例外五){

            的System.out.println(错误=+ e.getMessage());
            返回null;
        }
        返回 ;

    }

    @覆盖
    保护无效onProgressUpdate(空... unsued){

    }

    @覆盖
    保护无效onPostExecute(字符串sResponses){
        尝试 {

            如果(sResponses!= NULL){

                JSONObject的jsonObjSend =新的JSONObject(sResponse.toString());

                如果(jsonObjSend.getString(状态)。等于(成功)){

                    //另一个code
                }否则,如果(jsonObjSend.getString(状态)。等于(失败)){

                    //另一个code
                }

            }
        }赶上(例外五){
            Toast.makeText(getApplicationContext(),excepttion,Toast.LENGTH_LONG).show();
            Log.e(e.getClass()的getName(),e.​​getMessage(),E。);
        }
    }
}
 

(2)首先删除所有的库/ jar文件从您的libs文件夹:

(3)找到以下库/瓶从互联网上的文件,并放在libs文件夹:

(4)添加的库卡罐:

(5)您的订单与出口的库/ jar文件Tick标志的相同如下:

好了,完成。只是这些简单的步骤,帮助你上传图片文件到服务器。 希望这些helphul您和他人。

In my android eclipse project, i want to upload image file with name and email fields to server.

But i got the following error : my logcat is following :

NoSuchFieldError - BasicHeaderValueFormatter.INSTANCE

E/AndroidRuntime(25348): Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE

my entire logcat :

E/AndroidRuntime(25348): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(25348): Process: com.example.uploadfiles, PID: 25348
E/AndroidRuntime(25348): java.lang.RuntimeException: An error occured while    executing doInBackground()
E/AndroidRuntime(25348):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(25348):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(25348):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(25348):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(25348):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(25348):    at       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(25348):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(25348):    at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(25348): Caused by:  java.lang.NoSuchFieldError:  org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
E/AndroidRuntime(25348):    at org.apache.http.entity.ContentType.toString(ContentType.java:153)
E/AndroidRuntime(25348):    at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
E/AndroidRuntime(25348):    at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
E/AndroidRuntime(25348):    at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
E/AndroidRuntime(25348):    at com.example.uploadfiles.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:172)
E/AndroidRuntime(25348):    at com.example.uploadfiles.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:1)
E/AndroidRuntime(25348):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(25348):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(25348):    ... 4 more

My code for uploading image and some fields are following :

class ImageUploadTask extends AsyncTask<Void, Void, String> {
    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(Void... unsued) {
        try {

            File image = new File(iPath);
            FileBody fileBody = new FileBody(image);

            HttpClient client = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost post = new HttpPost(Constant.signUp);

            post.setHeader("enctype", "multipart/form-data");

            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            multipartEntity.addPart("ID", new StringBody("1"));
            multipartEntity.addPart("pID", new StringBody("1"));
            multipartEntity.addPart("userPhoto", fileBody);
            multipartEntity.addPart("email", new StringBody("joseph@gmail.com"));
            multipartEntity.addPart("cell", new StringBody("1234567890"));
            multipartEntity.addPart("username", new StringBody("joseph"));

            post.setEntity(multipartEntity.build());
            // post.setEntity((MultipartEntityBuilder) multipartEntity);
            HttpResponse response = client.execute(post, localContext);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
                    "UTF-8"));

            HttpResponse responses = client.execute(post);
            String responseBody = EntityUtils.toString(response.getEntity());
            Log.v("multiPartPost HTTP Response", responseBody);

        } catch (Exception e) {

            System.out.println("error=" + e.getMessage());
            return null;
        }
        return "";

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(String sResponse) {
        try {

            if (sResponse != null) {
                JSONObject JResponse = new JSONObject(sResponse);
                int success = JResponse.getInt("SUCCESS");
                String message = JResponse.getString("MESSAGE");
                if (success == 0) {
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Photo uploaded successfully", Toast.LENGTH_SHORT)
                            .show();
                    caption.setText("");
                }
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

My libs folder have following necessary libraries :

And in my java build path :

and in order-export in properties

Now i what have to do to solve this error : i working entire day for solving this but not, so please any one suggest me to how to solve this error, it very appreciated, thank you.

解决方案

This is main problem of libs., that all might faced, wrong library causes this issue,

So, following step helpful to you :

(1) Code : Following code is successfully upload photo to the server :

     btnUpload.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (Constant.bitmapPicture != null) {
                StoreByteImage(Constant.bitmapPicture, 90, "my_image");

            } else {
                Toast.makeText(getApplicationContext(), "image null", Toast.LENGTH_LONG).show();
            }
        }
    });

StoreByteImage(...) :

   public boolean StoreByteImage(Bitmap bitmap, int quality, String expName) {

    FileOutputStream fileOutputStream = null;
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File myNewFolder = new File(extStorageDirectory + "/Example");
    if (myNewFolder.mkdirs()) {
        myNewFolder.mkdir();
    }
    try {


        iPath = myNewFolder + "/" + expName + ".jpg";
        fileOutputStream = new FileOutputStream(myNewFolder + "/" + expName + ".jpg");
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        bitmap.compress(CompressFormat.JPEG, quality, bos);

        new ImageUploadTask().execute();

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

Class ImageUploadTask

class ImageUploadTask extends AsyncTask<Void, Void, String> {

    String sResponse;

    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            //

            File image = new File(iPath);
            FileBody fileBody = new FileBody(image);

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(YourImageUploadURLHere);

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Constant.bitmapPicture.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();

            entity.addPart("ID", new StringBody("1"));
            entity.addPart("pID", new StringBody("1"));
            entity.addPart("userPhoto", fileBody);
            entity.addPart("email", new StringBody("email@gmail.com"));
            entity.addPart("cell", new StringBody("1234567890"));
            entity.addPart("username", new StringBody("name"));

            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
                    "UTF-8"));

            sResponse = reader.readLine();
            System.out.println("responce=" + sResponse);

        } catch (Exception e) {

            System.out.println("error=" + e.getMessage());
            return null;
        }
        return "";

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(String sResponses) {
        try {

            if (sResponses != null) {

                JSONObject jsonObjSend = new JSONObject(sResponse.toString());

                if (jsonObjSend.getString("status").equals("success")) {

                    // another code
                } else if (jsonObjSend.getString("status").equals("fail")) {

                    // another code
                }

            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

(2) First Remove All libs/jar file from the your libs Folder :

(3) Find Following libraries/jar file from the internet and put in libs folder :

(4) Add jar from the libraries tab :

(5) Your Order and Export libraries/jar file Tick mark same as following :

Ok, Done. Just these simple steps helpful to you upload image file to server. hope these helphul to you and others.